Subject: bug#26741: Numeric sort in ls

From: Tony Malykh <[email-masked]>
Date: Mon, 1 May 2017 17:50:31 -0700

Hi all,

I am wondering if there is a way to do a numeric sort in ls? I am
aware of version sort option -v and blow I explain the problem with
it.

What I'd like to achieve:
Suppose I have files with numbers in their names.:
p1.py
p2.py
p11.py

It appears that version sort (-v) option achieves exactly that.
However, the problem with it is that it seems to be incompatible with
extension sort (-X) option. This limitation seems artificial to me: it
is fairly easy to sort files by the extension, and then sort them
numerically within every extension group.

If these files are sorted lexicographically, p2.py would come on the
last position. I would like p2.py to appear in between p1.py and
p11.py.

Maybe I don't understand something about the version sort algorithm;
maybe it does something more than numeric sort. But in this case, is
it possible to add a new flag for numeric sort, that is one that would
treat digits as numbers and would not conflict with the extension sort
-X ?

Here is a concrete example.

Here are some files
$ ls -1
1.py
11.py
2.pl
2.py

Desired output:
$ ls -1 --<magic-options>
2.pl
1.py
2.py
11.py
# Note that 2.pl goes to the top because of its extension.
# Note also that the *.py files are sorted numerically and not
lexicographically.

Attempt #1:
$ ls -1Xv
1.py
2.pl
2.py
11.py
# Note that the files are sorted numerically, but the extension sort
(-X) option seem to have been ignored, since the 2.pl file is among
the *.py files.

Attempt #2:
$ ls -1vX
2.pl
1.py
11.py
2.py
# Now the extension sort is working, but the files are sorted
lexicographically and not numerically.

Here is my version:
$ ls --version
ls (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)

Any suggestions will be appreciated!
Thanks
Tony