[Haskell-cafe] Tuesday Morning Games

Ketil Malde Ketil.Malde at bccs.uib.no
Tue Nov 21 07:36:40 EST 2006


Matthias Fischmann wrote:
>> sortBy ((. tail . fst) . compare . tail . fst)
>>     
>
> this would actually be a nice thing to have in the standard libraries:
>
> sortOn :: (Ord b) => (a -> b) -> [a] -> [a]
> sortOn f = sortBy $ (.f) . compare . f
I think the recommended way is to define

    on f g = \x y -> f (g x) (g y)

That way, you can do

    sortBy (compare `on` snd) [(1,2),(2,1)]
    => [(2,1),(1,2)]


-k


More information about the Haskell-Cafe mailing list