I’d like to sort by one property and then by another (if the first property is the same.)
What’s the idiomatic way in Haskell of composing two comparison functions, i.e. a function used with sortBy?
Given
f :: Ord a => a -> a -> Ordering
g :: Ord a => a -> a -> Ordering
composing f and g would yield:
h x y = case v of
EQ -> g x y
otherwise -> v
where v = f x y
vitus points out the very cool instance of
MonoidforOrdering. If you combine it with the instanceinstance Monoid b => Monoid (a -> b)it turns out your composition function is just (get ready):Check it out:
+1 for powerful and simple abstractions