> module Sort (sort, filesort) where > import Type_Data Sort a list in the appropriate direction with a given sort function > do_sort :: SortOrder -> (a -> a -> Bool) -> [a] -> [a] > {-# SPECIALIZE do_sort :: SortOrder -> (FileInfo -> FileInfo -> Bool) > -> [FileInfo] -> [FileInfo] #-} > do_sort d f xs = sort (\x y -> xor (d == Desc) (f x y)) xs > where xor a b = (not a && b) || (a && not b) Uses mergesort to sort a list with a sort function > mergesort :: (a -> a -> Bool) -> [a] -> [a] > mergesort _ [] = [] > mergesort _ [x] = [x] > mergesort f xs = merge (mergesort f ys) (mergesort f zs) > where (ys,zs) = splitAt (div (length xs) 2) xs > merge ls [] = ls > merge [] rs = rs > merge (l:ls) (r:rs) = if f l r then l:(merge ls (r:rs)) > else r:(merge (l:ls) rs) Sort a list, allows you to easily use different sort functions > sort :: (a -> a -> Bool) -> [a] -> [a] > sort f xs = mergesort f xs Sort a list of FileInfos in the appropriate direction against the appropriate field > filesort :: MyOptions -> SortOrder -> [FileInfo] -> [FileInfo] > filesort [] so xs = do_sort so sort_by_name xs > filesort ((Sort x):_) so xs = case x of > "time" -> do_sort so sort_by_date xs > "size" -> do_sort so sort_by_size xs > "extension" -> do_sort so sort_by_ext xs > "none" -> if so == Asc > then xs > else reverse xs > _ -> do_sort so sort_by_name xs > filesort (_:os) so xs = filesort os so xs Functions to sort by various information > sort_by_name :: FileInfo -> FileInfo -> Bool > sort_by_name (_,_,_,_,f1,_) (_,_,_,_,f2,_) = > (f1 < f2) > sort_by_date :: FileInfo -> FileInfo -> Bool > sort_by_date (_,_,_,d1,f1,_) (_,_,_,d2,f2,_) = > (d1 > d2) || ((d1 == d2) && (f1 < f2)) > sort_by_size :: FileInfo -> FileInfo -> Bool > sort_by_size (s1,_,_,_,f1,_) (s2,_,_,_,f2,_) = > (s1 > s2) || ((s1 == s2) && (f1 < f2)) > sort_by_ext :: FileInfo -> FileInfo -> Bool > sort_by_ext (_,_,_,_,f1,_) (_,_,_,_,f2,_) = > (e1 < e2) || ((e1 == e2) && (f1 < f2)) > where ext f = if or (map ((==) '.') f) > then reverse $ takeWhile ((/=) '.') $ reverse f > else "" > e1 = ext f1 > e2 = ext f2