Make C # ParallelEnumerable.OrderBy robust sorting

I sort the list of objects by their integer identifiers in parallel using OrderBy . I have several objects with the same identifier and need sorting to be stable.

According to Microsoft documentation , parallelized OrderBy unstable, but there is an implementation approach to make it stable. However, I cannot find an example of this.

 var list = new List<pair>() { new pair("a", 1), new pair("b", 1), new pair("c", 2), new pair("d", 3), new pair("e", 4) }; var newList = list.AsParallel().WithDegreeOfParallelism(4).OrderBy<pair, int>(p => p.order); private class pair { private String name; public int order; public pair (String name, int order) { this.name = name; this.order = order; } } 
+4
source share
1 answer

Remarks for another OrderBy method offer this approach:

 var newList = list .Select((pair, index) => new { pair, index }) .AsParallel().WithDegreeOfParallelism(4) .OrderBy(p => p.pair.order) .ThenBy(p => p.index) .Select(p => p.pair); 
+8
source

All Articles