[Haskell-cafe] A small oversight

Leon Smith leon.p.smith at gmail.com
Sun Feb 21 01:59:20 EST 2010

On Sat, Feb 20, 2010 at 5:47 AM, Andrew Coppin
<andrewcoppin at btinternet.com> wrote:

>  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 <haskell at benmachine.co.uk> wrote:
> 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



More information about the Haskell-Cafe mailing list