[Haskell-cafe] version sorting?

Javran Cheng javran.c at gmail.com
Sat Dec 8 01:47:44 UTC 2018


Hi Cafe,

I'm wondering if there's any existing packages that do "version sorting":
as an example, for a list like:

["foo-10.20", "foo-1.2", "foo-2.100", "foo-1.12"]

I'd like the result to be:

["foo-1.2", "foo-1.12", "foo-2.100", "foo-10.20"]

I could think of an implementation that turns String elements of a list into
[Either Int String] by grouping consecutive chunks using span and isDigit
and then do a sortOn:

versionSort :: [String] -> [String]
versionSort = sortOn brkND
  where
    tr f g (x,y) = f x : g y
    -- ND for non-digits
    brkND = tr Right brkD . span (not . isDigit)
    brkD = tr (Left . read @Int) brkND . span isDigit

(side question: does the helper function tr I defined above have a commonly
known name?)
just wondering if there are more sophisticated solutions available.

Best,
Javran
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20181207/dece7e37/attachment.html>


More information about the Haskell-Cafe mailing list