In this question one of the suggestions is to sort a list by Random.Next().
I assume (maybe incorrectly) he's suggesting this
public static IEnumerable<T> RandomSort<T>(this IEnumerable<T> items)
{
Random r = new Random();
var a = items.ToArray();
Array.Sort(a, (t1, t2) => (r.Next()%2==0)?-1 : 1);
return a;
}
(Yes, there already is an Array.RandomShuffle function which you would obviously use instead. That's not the question)
EDIT: The poster has clarified the answer. He was suggesting the use of the OrderBy clause, which would obviously be safe
The question is, is the above code (Using Array.Sort()) safe to run?
My issue is that it will be breaking a fundamental law of sorting predicates:
if (a < b) and (b < c) then (a < c)
It's not even guaranteeing that if (a < b) then ( a < b) the next time you ask.
Would this would take you into "undefined behaviour" territory?
For example, it could crash or fall into an infinite loop depending upon the sequence of numbers that Random() returns?