[Toybox] sort -V

enh enh at google.com
Wed Dec 19 13:01:46 PST 2018


On Wed, Dec 19, 2018 at 12:49 PM Rob Landley <rob at landley.net> wrote:
>
> On 12/19/18 2:14 PM, enh wrote:
> >> What is the _context_ of the users of sort -V? If I do try to implement
> >> something, what does success look like here? (Do we have a sort.test patch we
> >> can start with?)
> >
> > from most legit to least legit...
> >
> > someone trying to find the latest kernel version from git tags:
> >
> > https://android.googlesource.com/platform/tools/repohooks/+/master/tools/checkpatch.pl-update
> >
> > there also appears to be something directly in the kernel:
> >
> > https://android.googlesource.com/kernel/common/+/android-3.10/scripts/depmod.sh
> >
> > plus the usual suspects sort a directory of files called 0_foo, 1_bar,
> > etc, (where the number always comes first) apparently with no need for
> > -V at all.
> >
> > so really just kernel version tags?
>
> My horrible regex was every package aboriginal linux was using plus all the ones
> in Linux From Scratch and a reasonable chunk of beyond linux from scratch.
>
> Hmmm... Ok, it sounds like what it needs is a sort where:
>
> A) proceed through non-numeric bits sorted alphabetically, and both "-" and "."
> are considered non-numeric.
>
> B) groups of digits are sorted numerically. (So if isdigit(*a) && isdigit(*b)
>
> That would handle most name-1.234-pre6.5b.tgz variants. The hitch being that
> "1.234-pre6 comes _before_ 1.234", as would "1.234-rc2" (I forget what -rc vs
> -pre was but it's historical and they don't do that no more.) Aha, but '-' is
> less than '0' so if we alphasort when we _don't_ have two groups of digits, that
> comes out ahead properly.
>
> But "1.234" vs "1.234-rc1" and "1.234b" wanna go different ways. Ok, when one
> string ends early, compare the first leftover character with the last character
> o the previous string. - is <0, a is >9.
>
> Let's see what "git describe" spits out...
>
>   $ git describe
>   v4.20-rc7-10-g62393db
>
> Hmmm... 4.20-rc7 is before 4.20, but 4.20-rc7-10 is _after_ 4.20-rc7.
>
> Grumble grumble. It's all horrible heuristics, but I would like to find some
> simple horrible heuristics that work with the cases we care about.

doesn't that then mean that the world has *4* variants of this?
(strverscmp, coreutils, debian packages, and toybox?)

(actually, autoconf already has a hacked-up replacement with a comment
about `sort -V` not being universally available.)

but don't listen to me... something is better than nothing :-)

> (I could just hardwire in -rc and -pre...)
>
> Rob


More information about the Toybox mailing list