Sort using a custom comparator: Difference between revisions
m
→{{header|Haskell}}: Applied Ormolu, tidied
m (→{{header|Haskell}}: Applied Ormolu, tidied) |
|||
Line 1,726:
=={{header|Haskell}}==
{{works with|GHC}}
<lang haskell>import Data.
import Data.Char (toLower)▼
import Data.List (sortBy)
-------------------- CUSTOM COMPARATORS ------------------
lengthThenAZ :: String -> String -> Ordering
lengthThenAZ = comparing length
descLengthThenAZ :: String -> String -> Ordering
descLengthThenAZ =
flip (comparing length)
<> comparing (fmap toLower)
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_
putStrLn
( fmap
unlines
( [sortBy] <*> [lengthThenAZ, descLengthThenAZ]
"are",
</lang>▼
"some",
"sample",
"strings",
"to",
"be",
"sorted"
]
]
)
▲ )</lang>
{{Out}}
<pre>be
|