score:7

Accepted answer

put simply, he's wrong. the linq orderby et al. methods are documented as performing a stable sort:

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.

score:0

he's saying if do, say orderby lastname only then anybody with the same last name will be sorted in an unpredictable order. so if you combine it with, say orderby lastname, firstname you can make the order predictable again (except for people with the same first and last name of course).

at least, that's what i would assume.

score:5

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.


Related Query

More Query from same tag