使C#ParallelEnumerable.OrderBy稳定排序 [英] Make C# ParallelEnumerable.OrderBy stable sort
本文介绍了使C#ParallelEnumerable.OrderBy稳定排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用OrderBy
通过对象的整数ID并行排序对象列表.我有几个具有相同ID的对象,需要排序稳定.
I'm sorting a list of objects by their integer ids in parallel using OrderBy
. I have a few objects with the same id and need the sort to be stable.
根据 Microsoft的文档,并行化的OrderBy
不是稳定,但是有一种实现方法可以使其稳定.但是,我找不到这样的例子.
According to Microsoft's documentation, the parallelized OrderBy
is not stable, 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;
}
}
推荐答案
另一种OrderBy
方法提出了这种方法:
The remarks for the other OrderBy
method suggest 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);
这篇关于使C#ParallelEnumerable.OrderBy稳定排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文