• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

List<> shuffle

I have a Generic List (List<>). How is best to shuffle it? (randomize its items)
0
johnkainn
Asked:
johnkainn
1 Solution
 
p_davisCommented:
maybe add a property on your object that is a random number and then sort the list by that.
0
 
gregoryyoungCommented:
that would take the time of the sort ...

you could randomize it in linear time by just removing/adding the node at a random spot.

for(int i=0;i<list.length;i++) {
int newIndex = (int) Random.Next * SizeOfList - 1;
list.RemoveAt(i);
list.InsertAt(newIndex);
}

Cheers,

Greg
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now