RosettaCodeData/Task/Sort-an-array-of-composite-structures/Haskell/sort-an-array-of-composite-structures-1.hs
2017-09-25 22:28:19 +02:00

26 lines
493 B
Haskell

import Data.List
import Data.Function (on)
data Person =
P String
Int
deriving (Eq)
instance Show Person where
show (P name val) = "Person " ++ name ++ " with value " ++ show val
instance Ord Person where
compare (P a _) (P b _) = compare a b
pVal :: Person -> Int
pVal (P _ x) = x
people :: [Person]
people = [P "Joe" 12, P "Bob" 8, P "Alice" 9, P "Harry" 2]
main :: IO ()
main = do
mapM_ print $ sort people
putStrLn []
mapM_ print $ sortBy (on compare pVal) people