What Sorting Algorithm Is Used By LINQ “OrderBy”?

て烟熏妆下的殇ゞ 提交于 2019-11-26 17:47:38

For LINQ to Objects, it's a stable quicksort that is used. For any other kind of LINQ, it's left to the underlying implementation.

Boot up reflector, open to System.Linq.EnumerableSorter reveals that Linq2Objects uses the quick sort

Quicksort is used, but the reason why it is stable is because the indices of each pair of elements are compared if all the keys test equal.

In other words, you can make any quicksort stable by including in the comparator function a comparison of the original indices of the two elements as a fallback.

Source: http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,1395017e067e5a34

I am under the understanding that OrderBy gets translated into SQL that performs the sort on the Database. At least in the case of LINQ to SQL

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!