
On Sat, Feb 20, 2010 at 5:47 AM, Andrew Coppin
sortOn :: (Ord y) => (x -> y) -> [x] -> [x] sortOn foo = sortBy (compare `on` foo)
Incidentally, this function is provided as Data.List.Ordered.sortOn'
in the data-ordlist package...
On Sat, Feb 20, 2010 at 7:39 AM, Ben Millwood
But it would still be useful to have sortOn et al to capture the common technique when your sorting property is potentially expensive (sortOn length, for example):
sortOn f = map fst . sortBy (comparing snd) . map (\x -> (x, f x))
a technique which I believe is called a Schwar[t]zian transform.
An older name for this technique is "decorate-sort-undecorate". Data-ordlist also provides this as Data.List.Ordered.sortOn http://hackage.haskell.org/package/data-ordlist Best, Leon