To work correctly, the sorting algorithm should not depend on the state of the mutation. An explanation of why using a random generator when comparing values is not a good idea is given here .
The problem can be solved in two ways:
Option 1: Precomputing Random Numbers
var tmp = goals.Select( g=> new {goal = g, weight = rnd.NextDouble()}) .OrderByDescending(t=>t.goal.Active)
Working example
Option 2: Sort and then shuffle links
Random rnd = new Random(); Comparison<Goal> comparison = (a, b) => { // first chooses the Active ones (if any) var sort = b.Active.CompareTo(a.Active); if (sort == 0) { // then sort by level return sort = (a.Level).CompareTo(b.Level); } else { return sort; } }; int startIndex = 0; int endIndex = 0; goals.Sort(comparison); while (startIndex < goals.Count) { for (endIndex = startIndex + 1; endIndex < goals.Count; ++endIndex) { if (comparison(goals[startIndex], goals[endIndex]) != 0) { //End of tie break; } } if (endIndex - startIndex > 1) { // Shuffle goals of the same level ShuffleRange(goals, startIndex, endIndex - startIndex, rnd); } startIndex = endIndex; } static void ShuffleRange<T>(List<T> list, int startIndex, int count, Random rnd) { int n = startIndex + count; while (n > startIndex + 1) { int k = rnd.Next(startIndex, n--); T value = list[k]; list[k] = list[n]; list[n] = value; } }
Working example
Shuffle algorithm borrowed from here
alexm source share