how was Array.Sort implemented in .NET?

后端 未结 6 554
遇见更好的自我
遇见更好的自我 2021-01-13 21:28

I am using structures in my programming and I sort the structure according to a value in the structure using IComparer.

How did Microsoft implement th

6条回答
  •  轮回少年
    2021-01-13 22:23

    Array.Sort is an unstable sort, so the order of elements which are the same is undefined and not conserved. The article on Array.Sort in MSDN states:

    This method uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

    LINQ's OrderBy methods on the other hand are stable. The article on OrderBy in the MSDN states:

    This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.

提交回复
热议问题