Ord comparing, but returns the smallest one
You can make use of Down
which reverses the order of comparing:
import Data.Ord(Down(Down))
(maximumBy (comparing length <> comparing (Down . head))) sx
or you can flip the two operands when you are comparing the value:
(maximumBy (comparing length <> flip (comparing head))) sx
That being said, you should be careful with comparing head
. Empty lists have no head, so that can result in an error if you are comparing two empty lists.
You can, like @DanielWagner says, make use of take 1
. This works because lists [a]
are an instance of Ord
as well, given a
is an instance of Ord
. In that case the lists are ordered lexicographically. For lists with one or more elements, this thus means that we order by the first element:
maximumBy (comparing length <> flip (comparing (take 1))) sx
This is why the Down
newtype
exists: to reverse ordering. Use it like this:
import Data.Ord
(maximumBy (comparing length <> comparing (Down . head))) sx