module Sort where
import System.Environment (getArgs)
import Data.List (group, sort, intercalate, (\\))
import Control.Monad (when)
import Data.Char (isSpace)


-- Rename main' to main to use it as a standalone program.
-- (or change the Makefile to use -main-is ...)
main' :: IO ()
main' = do
    args <- getArgs
    when (length args < 1) $
        error "No filename given"
    s <- readFile (head args)
    putStrLn (sortString s)


-- Returns sorted string. Remove all special whitespace chars
sortString :: String -> String
sortString s = 
    let fixed = filter (not . isSpace) s
        all   = (group . sort) fixed
    in intercalate "\n" all
 

