Data.List.sortFun
I've found these two functions useful often enough to suggest adding it to Data.List:
{-# RULES
"sortFun/fst" forall . sortFun fst = sortFun' fst
"sortFun/snd" forall . sortFun snd = sortFun' snd
#-}
sortFun :: Ord b => (a -> b) -> [a] -> [a]
sortFun f = map snd . sortFun' fst . map (\x -> (f x, x))
sortFun' :: Ord b => (a -> b) -> [a] -> [a]
sortFun' f = Data.List.sortBy (\x y -> compare (f x) (f y))
I am not terribly fond of the name "sortFun". If anybody has a better suggestion...
Trac metadata
| Trac field | Value |
|---|---|
| Version | 6.8.3 |
| Type | FeatureRequest |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | libraries/base |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | Unknown |
| Architecture | Unknown |