6 ms·
> It's much better to have comparator functions for the most common types baked in, and use projection to select a list of fields to use to compare with. Excep
by masklinn 14y ago
> It's much better to have comparator functions for the most common types baked in, and use projection to select a list of fields to use to compare with.
Except that requires a sorting key for the result. Trivial for dynamically typed languages, but how do you handle the type of that key in a statically typed language? Mandate that the key be a string? Build a limited set of overloads against a dedicated type? And even then, how do you specify the relative constraints of the different values within that key, especially within the limitations of java's type system?
> The example given here - ".sorted((a, b) -> a.getValue() - b.getValue())" - won't work properly when the calculation wraps around.
But that's less an issue of "comparator functions [being] very easy to get wrong" and more an issue of "integer is an asinine type to use as ordering result". Even more so in a statically typed language. Haskell uses a dedicated enumerated type[0] which does not have this issue.
[0] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Ord.html#t:Ordering http://hackage.haskell.org/packages/archive/base/latest/doc/...
- barrkel 14y agoI don't know what you mean by "sorting key" in this context. The idiom I have in mind is how .Net implements "SortBy", "ThenBy" and friends. Overloads can be used for a comparator function based approach, but it should not be the first choice. And I'll stand by comparators being easy to get wrong, particularly when values may be null, polymorphic, etc.
- batista 14y ago>I don't know what you mean by "sorting key" in this context. He means the item by which you sort. The "by" in your "SortBy" example.
- barrkel 14y agoYes, but that reading doesn't make sense given what he also wrote - that apparently this would make things more awkward in a static language.
- batista 14y agoHow do you define the sorting key in a static language? With reflection? e.g if anArray is a Java array of Employee objects, and you want to sort on their salary attribute, how do you specify it? anArray.sortBy(???); In a dynamic language it doesn't matter much, since all attribute access amounts to (and is as slow as) reflection on a static language anyway, so you can do something like, say: anArray.sortBy("salary");
- Peaker 14y agoIn Haskell, sortBy takes a comparison function: sortBy :: (a -> a -> Ordering) -> [a] -> [a] And Ordering is defined as: data Ordering = LT | EQ | GT If you want to provide a "key" function rather than a comparison function, you can use sortOn: sortOn :: Ord b => (a -> b) -> [a] -> [a] Which is basically just a sortBy with a function that applies the key function and then compares as normal.
- eru 14y agoAnd everything is statically typed.
- spullara 14y agoIn JDK8 there are method references that could enable this. For example, you can reference a method in a class like Person::age ( equivalent to (Person person) -> person.age() ) that could then be used to sort the collection since Integer implements Comparable.
- pbz 14y agoAnArray.Sort(e -> e.Salary) and if there's no built-in way to compare two Salary objects (let's say they're complex objects), then you come up with an interface that the sorting function would use and the objects would implement. This way you let the objects compare themselves.
- 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.
- heretohelp 14y agoI have to agree here, this feels too much like: "Doctor, it hurts when I move my arm like this!" "So don't move your arm like that." We've spent the last...god knows how many decades dealing with wrap-around on (unsigned generally) integers. :|
- eru 14y ago> Trivial for dynamically typed languages, but how do you handle the type of that key in a statically typed language? Haskell handles this just fine. Static types are only a problem, if they are weak and/or your language syntax is cumbersome. Automatic type inferences helps, of course.