[Haskell-cafe] Comparing on multiple criteria
Martijn van Steenbergen
martijn at van.steenbergen.nl
Sun Dec 21 08:52:07 EST 2008
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!)
To reverse the order of a criterion or a set of criteria, flip can be used:
compareTuple' = mconcat [comparing fst, flip $ comparing snd]
I think it would be really neat if these two use cases were also
described in comparing's documentation. Who is the right person to ask
this of?
Thanks in advance,
Martijn.
More information about the Haskell-Cafe
mailing list