1

I have to make a list containing objects. And the objects need to be in random order. Here I give them random numbers:

Random tal = new Random(); list1[i].nummer = tal.Next(list1.Count); listGold.Add(list1[i]); 

And now i just need to order them by number. Which I thought linq could do for me. But it can't :S

I am trying this:

RepeaterSponsorGold.DataSource = listGold.OrderBy(n => n.nummer); RepeaterSponsorGold.DataBind(); 

to order my list by nummer and to put the list into my repater. But the lsit doesn't seem to be sorted... or doesn't seem to get random numbers. i don't know which. Can anybody see what i am doing wrong??

4
  • 2
    Ordering by random can have a weird distribution. You should use en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle Commented May 21, 2012 at 13:34
  • 3
    Is the new Random() being created inside the loop that you are using to set the random number? You should onyl create a new Random() once and then use that instance from then on, otherwise you will likely be getting the same random number out repeatedly. Commented May 21, 2012 at 13:34
  • Step through your code and make sure that your random numbers are set correctly. Commented May 21, 2012 at 13:35
  • What is the value of list1.Count? What does your list contain in the nummer field? Commented May 21, 2012 at 13:35

5 Answers 5

3

Try

RepeaterSponsorGold.DataSource = listGold.OrderBy(n => n.nummer).ToList(); 
Sign up to request clarification or add additional context in comments.

4 Comments

How does ToList() help with sorting?
How does ToList() help with binding?
I believe Marko's answer is right, it is the .ToList() or .ToArray() that forces the enumerable to execute the OrderBy statement. Otherwise it hasn't processed it yet.
It's true that deferred execution could be in play. However, it would also be executed when the Repeater enumerates the DataSource, right?
2

If you need to sort in random order you could try:

var listGold = list1.OrderBy(n => Guid.NewGuid()); 

without the need to use Random.

2 Comments

This is not a great solution if performance is a concern. Sorting is an O(n*log(n)) operation. Shuffling only needs to be O(n). The 'shuffling algorithm' link shows a better way.
It's also not a great solution if randomness is a concern! Guids are designed to be unique, they're not necessarily random.
2

You should't use Random() to shuffle a list, as it can produce determanistic distributions.

Instead, use a suffle:

Randomize a List<T>

public static void Shuffle<T>(this IList<T> list) { Random rng = new Random(); int n = list.Count; while (n > 1) { n--; int k = rng.Next(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } } .... listGold.Shuffle(); 

4 Comments

You should pass Random to the method, so your program ever only uses one Random.
Why your getting upvotes, who knows - the OP's question isn't how to randomize the list, but how to sort it.
@Chuck: "I have to make a list containing objects. And the objects need to be in random order." is pretty unambiguous.
I'm pretty sure the concept of taking a list of ordered items, and randomizing said list is known as "shuffling"
0

How about this:

List<string> list = new List<string>() { "Blue", "Brown", "Beige", "Red", "Black" }; Random random = new Random(); var orderedList = list.Select(c => new {Item = c, Order = random.Next()}) .OrderBy(i=>i.Item) .Select(x=>x.Item).ToList(); orderedList.ForEach(x=>Console.WriteLine(x)); 

Comments

0

You have a couple of issues.

The most important is that you should only ever have a single instance of Random, and use it repeatedly, rather than invoking a new one every time.

Second, by using the list count as your seed, you'll get a roughly sequential list.

list1[i].nummer = tal.Next(list1.Count); 

Because you're using your list.Count as your seed you'll get a list that is roughly in the order they were added.

Instead try:

Enumerable.Range(0,100) .ToDictionary(k=> Guid.NewGuid()) .OrderBy(o => o.Key).Select(s => s.Value).ToList() 

Using the Guids will give you a more natural randomization than Random.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.