Unstable sort means that a chain of x.OrderBy(...).OrderBy(...)
calls will only reliably sort according to the final criterion. x.OrderBy(...).ThenBy(...)
explicitly captures knowledge of the previous sort order when applying the new sort order. I believe it does this by calling IOrderedEnumerable<TElement>.CreateOrderedEnumerable<TKey>
, though I'm not 100% sure of this.
EDIT: Just to be clear, when I say, "captures the knowledge..." I don't mean to suggest that the first OrderBy performs a sort, and somehow the second one knows what it did. Remember that OrderBy
returns an IOrderedEnumerable<T>
, which doesn't perform any work at all until someone tries to consume the elements. In this scenario, it will never perform the sort, since ThenBy
, using knowledge of how OrderBy
would have sorted, constructs a brand new sorter that applies both sort orderings in the expected manner and in a single step.
It has been pointed out that Magennis is wrong on the unstable sort thing. The above description is still valid, however.