3 ms·
> I don't know what you mean by "sorting key" in this context. The result of the projection which is associated with each value and used to actually sort the v
by masklinn 14y ago
> I don't know what you mean by "sorting key" in this context.
The result of the projection which is associated with each value and used to actually sort the values.
> The idiom I have in mind is how .Net implements "SortBy", "ThenBy" and friends.
I assume you mean OrderBy. I see, instead of having sorting as a single atomic operation it becomes a stateful multi-step transformation of the iterator. I can still see weirdness in it: what are the constraints on the return value of the key selector functions? The MSDN does not list any, but that can't be right: what if the key selector returns a value which can not be sorted itself?
> And I'll stand by comparators being easy to get wrong, particularly when values may be null, polymorphic, etc.
Trivial key extraction does not make that any easier: instead of being hard, hard cases become impossible.
- barrkel 14y ago> > And I'll stand by comparators being easy to get wrong, particularly when values may be null, polymorphic, etc. > Trivial key extraction does not make that any easier: instead of being hard, hard cases become impossible. Perhaps you missed this bit: > > Overloads can be used for a comparator function based approach, but it should not be the first choice. Key extraction makes trivial cases less error prone than comparators; overloads can be used to make the hard cases possible. Comparators everywhere make all cases error-prone.
- moomin 14y agoFor the record, T needs to implement IComparable<T>. I think there are special rules for anonymous types for convenience. And do bear in mind that C# supports both idioms. So you can handle hard cases any way you want.