[Haskell-cafe] Dynamically altering sort order

Martijn van Steenbergen martijn at van.steenbergen.nl
Fri Apr 24 17:22:10 EDT 2009


Hi Denis,

Denis Bueno wrote:
> where the rCompare field would be a function that is based on the
> flags passed to the command-line problem.  But this has an ugly
> asymmetry.  Does anyone have any other ideas?

Here's a solution that is more symmetrical but not necessarily prettier:

newtype Wrap = Wrap { unwrap :: Record }

instance Ord Wrap where
   ... your compare function here ...

But I don't think there's anything wrong with your original solution. 
You can write your own sortBy and hide (not export) the CompareRecord type:

sortBy :: (a -> a -> Ord) -> [a] -> IO [a]
sortBy cmp = map unCR . sort . map (CompareRecord cmp)

HTH,

Martijn.


More information about the Haskell-Cafe mailing list