fork download
  1. qsort [] = []
  2. qsort (h:t) =
  3. qsort [x | x <- t, x <= h] ++ [h] ++ qsort [x | x <- t, x > h]
  4.  
  5. data Foo = Foo (Char, Char, Int) deriving (Eq, Show)
  6.  
  7. data Bar = Bar {
  8. ch1 :: Char,
  9. ch2 :: Char,
  10. ord :: Int
  11. } deriving (Eq, Show)
  12.  
  13. instance Ord Foo where
  14. compare (Foo (_, _, x)) (Foo (_, _, y)) = compare x y
  15.  
  16. instance Ord Bar where
  17. compare (Bar _ _ x) (Bar _ _ y) = compare x y
  18.  
  19. testDataFoo = [Foo ('d', 'k', 21), Foo ('a', 'b', 1), Foo ('x', 'y', 34)]
  20. testDataBar = [Bar 'd' 'k' 21, Bar 'a' 'b' 1, Bar 'x' 'y' 34]
  21.  
  22. main = do
  23. print $ qsort testDataFoo
  24. print $ qsort testDataBar
  25.  
Success #stdin #stdout 0s 8388607KB
stdin
Standard input is empty
stdout
[Foo ('a','b',1),Foo ('d','k',21),Foo ('x','y',34)]
[Bar {ch1 = 'a', ch2 = 'b', ord = 1},Bar {ch1 = 'd', ch2 = 'k', ord = 21},Bar {ch1 = 'x', ch2 = 'y', ord = 34}]