tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: sort --version-sort
On Tue, Feb 18, 2025 at 05:00:43PM +0100, ??? wrote:
> On Tue, Feb 18, 2025 at 09:10:44AM -0500, enh wrote:
> > fwiw, one reason i never bothered implementing strverscmp() for
> > Android is that (a) basic testing showed that the gnu documentation
> > didn't match their implementation and (b) no two existing
> > implementations appeared to behave the same.
>
> It's worth noting that coreutils version sort (ls, sort) is /not/
> strverscmp(), but a different, more complex algorithm
> (this is because strverscmp() actually sucks for sorting filenames:
> strverscmp q-1.2.1.tar < q-1.2.1a.tar < q-1.2.tar < q-1.2a.tar < q-1.2b.tar
> ls -v q-1.2.tar < q-1.2a.tar < q-1.2b.tar < q-1.2.1.tar < q-1.2.1a.tar
> as discovered by my brief misguided stint of actually using strverscmp
> https://git.sr.ht/~nabijaczleweli/voreutils/tree/74f986a51cdfa67587efa186726617539300551a%5E/item/cmd/ls.cpp#L710).
>
If the util is looking for different kinds of version suffixes, and
with a fixed set of patterns, then the whole point violates the
Unix principles. Since if one wants to sort this, he can use a pipe
involving ls(1) or find(1), sed(1) for splitting the fields, and
then POSIX.2 sort(1) for sorting according to fields and according of
the version pattern.
If there were to be additions to sort(1), it would be to allow several
directives of discontinuous ranges (given in precedence order, but not
necessarily in increasing field number order between ranges) and
allowing to specify, before the range, if the comparison is to be
numerical or alpha (and reverse or not)---but even this can be
implemented using tricks like frozing the initial portion of the
"already sorted" record (by changing the field separator so that the
portion remains unchanged as a whole; the only problem being to find
two separators that don't appear for sure in the record).
At least it's what seems to be logical to me.
--
Thierry Laronde <tlaronde +AT+ kergis +dot+ com>
http://www.kergis.com/
http://kertex.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89 250D 52B1 AE95 6006 F40C
Home |
Main Index |
Thread Index |
Old Index