sort func xs = let k = func $ head xs
                   as = [x' | x' <- xs, func x' < k]
                   bs = [x' | x' <- xs, func x' == k]
                   cs = [x' | x' <- xs, func x' > k]
               in
                   sort func as ++ bs ++ sort func cs
sort func xs = let k = func $ head xs
                   as = [x' | x' <- xs, func x' < k]
                   bs = [x' | x' <- xs, func x' == k]
                   cs = [x' | x' <- xs, func x' > k]
               in
                   sort func as ++ bs ++ sort func cs
sort func xs = let k = func $ head xs
                   as = [x' | x' <- xs, func x' < k]
                   bs = [x' | x' <- xs, func x' == k]
                   cs = [x' | x' <- xs, func x' > k]
               in
                   sort func as ++ bs ++ sort func cs