[Haskell-cafe] Comparing on multiple criteria

Jan-Willem Maessen jmaessen at alum.mit.edu
Sun Dec 21 11:20:02 EST 2008


On Dec 21, 2008, at 8:52 AM, Martijn van Steenbergen wrote:

> Hello all,
>
> Data.Ord has a handy function called comparing, and its  
> documentation shows an example of its use.
>
> But what if you want to sort a list of values based on multiple  
> criteria? It turns out there is a neat way to do this:
>
> compareTuple = mconcat [comparing fst, comparing snd]
>
> The default Monoid instances for Ordering and functions work exactly  
> as required here. (Thanks to vixey in #haskell for the hint to look  
> at monoids!)

Indeed, this is great to know.  I can't help but notice that there is  
no documentation of any kind at all for the Monoid instance of  
Ordering; how were we supposed to know this behavior existed in the  
first place, except by hunting down the source code for the instance  
declaration?

-Jan-Willem Maessen



More information about the Haskell-Cafe mailing list