is there already a function that does this?

-- Sort the [a]'s by the [b]'s. sortByKeys :: (Ord b) => [b] -> [a] -> [a] sortByKeys keys values = map snd $ sortBy compareFst (zip keys values) where compareFst x y = compare (fst x) (fst y) -- Sort a list by an IO-returning compare function. sortByM :: (Monad m, Ord b) => (a -> m b) -> [a] -> m [a] sortByM fun lis = mapM fun lis >>= \keys -> return $ sortByKeys keys lis Is there a function that can make sortByM out of sortByKeys? I don't know of one, but having been writing haskell for a few weeks, it sort of smells to me like there probably is one.

Those both look roughly like what I would do. -- Lennart On Mar 3, 2007, at 21:35 , Jared Jennings wrote:
-- Sort the [a]'s by the [b]'s. sortByKeys :: (Ord b) => [b] -> [a] -> [a] sortByKeys keys values = map snd $ sortBy compareFst (zip keys values) where compareFst x y = compare (fst x) (fst y)
-- Sort a list by an IO-returning compare function. sortByM :: (Monad m, Ord b) => (a -> m b) -> [a] -> m [a] sortByM fun lis = mapM fun lis >>= \keys -> return $ sortByKeys keys lis
Is there a function that can make sortByM out of sortByKeys? I don't know of one, but having been writing haskell for a few weeks, it sort of smells to me like there probably is one. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, 03 Mar 2007 22:35:00 +0100, Jared Jennings
-- Sort the [a]'s by the [b]'s. sortByKeys :: (Ord b) => [b] -> [a] -> [a] sortByKeys keys values = map snd $ sortBy compareFst (zip keys values) where compareFst x y = compare (fst x) (fst y)
You can simplify that to: sortByKeys :: (Ord b) => [b] -> [a] -> [a] sortByKeys keys values = map snd $ sort $ zip keys values as "sort" also handles tuples; it is not fully identical, in case there are duplicate keys: Data.List> sort [(2, 2), (2, 1), (1, 2), (1, 1)] [(1,1),(1,2),(2,1),(2,2)] -- Met vriendelijke groet, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ -- Using Opera's revolutionary e-mail client: https://secure.bmtmicro.com/opera/buy-opera.html?AID=789433

Jared Jennings:
-- Sort the [a]'s by the [b]'s. sortByKeys :: (Ord b) => [b] -> [a] -> [a] sortByKeys keys values = map snd $ sortBy compareFst (zip keys values) where compareFst x y = compare (fst x) (fst y)
Henk-Jan van Tuyl:
You can simplify that to:
sortByKeys :: (Ord b) => [b] -> [a] -> [a] sortByKeys keys values = map snd $ sort $ zip keys values
as "sort" also handles tuples; it is not fully identical, in case there are duplicate keys:
Data.List> sort [(2, 2), (2, 1), (1, 2), (1, 1)] [(1,1),(1,2),(2,1),(2,2)]
The modified version would require an ordering for the values as well as the keys, so it actually has a different type: sortByKeys :: Ord (b,a) => [b] -> [a] -> [a] The original didn't place any restrictions on the values.
participants (4)
-
Henk-Jan van Tuyl
-
Jared Jennings
-
Lennart Augustsson
-
Matthew Brecknell