显然LINQ的"OrderBy"最初被指定为不稳定,但到Orca时,它被指定为稳定.并非所有文档都已相应更新 - 请考虑以下链接:
但是,如果LINQ的OrderBy现在"稳定",那么这意味着它没有使用快速排序(这本质上是不稳定的),即使某些文档(例如Troy的书)说它是.所以我的问题是:如果不是快速排序,那么LINQ的orderBy使用的实际算法是什么?
我对List Sort方法如何处理排序有疑问.鉴于以下要素:
class Element : IComparable<Element>
{
public int Priority { get; set; }
public string Description { get; set; }
public int CompareTo(Element other)
{
return Priority.CompareTo(other.Priority);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试这样排序:
List<Element> elements = new List<Element>()
{
new Element()
{
Priority = 1,
Description = "First"
},
new Element()
{
Priority = 1,
Description = "Second"
},
new Element()
{
Priority = 2,
Description = "Third"
}
};
elements.Sort();
Run Code Online (Sandbox Code Playgroud)
然后第一个元素是先前的第二个元素"Second".或者,换句话说,这个断言失败了:
Assert.AreEqual("First", elements[0].Description);
Run Code Online (Sandbox Code Playgroud)
当元素基本相同时,为什么.NET重新排序我的列表?如果比较返回非零值,我希望它只对列表重新排序.