[Haskell-cafe] Dynamically altering sort order

Denis Bueno dbueno at gmail.com
Fri Apr 24 17:11:53 EDT 2009


Hi all,

Suppose I have the following interface to a sorting function:

    sort :: (Ord a) => [a] -> IO [a] -- sort large, on-disk array of records

but I don't have a sortBy where you can simply pass a compare function.

Wrapped around this is a command-line program which should allow the
user to specify different orderings on Records.  For example, if the
Record has three fields, the user should be able to say "sort only on
the first two".

Is there an Ord instance that can be dynamically changed in this way?

My first idea is something like this:

    data CompareRecord = CR{ rCompare :: Record -> Record -> Ordering,
unCR :: Record }
    instance Ord CompareRecord where
        compare (CR cmp x) (CR _ y) = cmp x y

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?

Thanks.
                              Denis


More information about the Haskell-Cafe mailing list