Javran Cheng
2018-12-08 01:47:44 UTC
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
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