I'd like to perform a sort based on a specific value within a record. As such I was thinking of passing a lens into a lensSort
function but I've been unable to make it work.
Ideally I could do something like this
lensSort :: HasLens a => Lens' a b -> a -> a -> -> Ordering
lensSort lens x y | x ^. lens > y ^. lens = GT
| x ^. lens < y ^. lens = LT
| otherwise = GT
And be able to call it with something like
data Rectangle = Rectangle { _height :: Int, _width :: Int }
makeLenses'' Rectangle
let foo = [Rectangle 1 2, Rectangle 2 1]
sortBy (lensSort height) foo
I'm failing to get this to work, and concerned I might be barking up the wrong tree completely, I'm still new to Haskell.