Created
November 15, 2013 12:03
-
-
Save fujimura/7483315 to your computer and use it in GitHub Desktop.
Sort CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ScopedTypeVariables #-} | |
module Main where | |
import Control.Monad.ST | |
import qualified Data.ByteString.Lazy as BL | |
import Data.Csv | |
import Data.List (intercalate) | |
import qualified Data.Vector as V | |
import qualified Data.Vector.Algorithms.Intro as VA | |
import System.Environment (getArgs) | |
main :: IO () | |
main = do | |
(fileName:_) <- getArgs | |
csvData <- BL.readFile fileName | |
case decode False csvData of | |
Left err -> putStrLn err | |
Right v -> go v | |
where | |
go :: V.Vector (String,String,String,String) -> IO () | |
go v = V.forM_ (sort v) $ \ (a,b,c,d) -> putStrLn $ intercalate "," [a,b,c,d] | |
sort v = runST $ do | |
mv <- V.thaw v | |
VA.sortBy compare mv | |
V.freeze mv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment