November Happy Hour will be moved to Thursday December 5th.

randomize a pagedatacollection

Vote:
 
Hi all I'm trying to randomize the content of a pagedatacollection and was wondernig if there is a sorter for this already, i.e FilterPropertySort or something alike. Or maybe put it all in an arraylist and randomize that one? Any suggestions?
#13161
Aug 15, 2007 10:25
Vote:
 
Well, since I figured it out (which was quite simple btw) I might aswell post it here and it might help someone out :) for (int i = 0; i < yourPageDataCollectionObject.Count; i++) { PageData x = yourPageDataCollectionObject[i]; Random rng = new Random(); int index = rng.Next(yourPageDataCollectionObject.Count - i) + i; yourPageDataCollectionObject[i] = yourPageDataCollectionObject[index]; yourPageDataCollectionObject[index] = x; }
#15483
Aug 15, 2007 13:49
Vote:
 

That code save me a lot of time, thanks! I did however have some trouble with some objects that as good as always seemed to get the same position after the randomization. I got the a tip in another thread to move the initializing of the Random class outside the loop and that solved it.

#35565
Dec 15, 2009 16:47
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.