Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Surprising behaviour with numeric glob sort
- X-seq: zsh-workers 41186
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Surprising behaviour with numeric glob sort
- Date: Wed, 31 May 2017 22:24:53 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mail-followup-to:mime-version :content-disposition:content-transfer-encoding:user-agent; bh=s4QJYeO64iNRO/20Oei5tk5VIb9RchFWmwrMWP8pxrA=; b=jiNUwPWuZQEY+cxJoRexk40YmF1t4ZQWJm66o9R0R3S7vt67rXnG6GHHQMcangJxbU /vhMZFimny5kHHWek+oRw030gVvvVh7glriF6kSJJbOe0ZSBsM/IvW9f2XL/QwmGBkiR xW33GPyC72dxqVD4lyZzklB0nN+WpITfx101WgL4Ne4DmQdhD2wcMKrexv64Lf97MyFs fuHB64bhx8iTUZCWvP4CrVnkymfNDcssiAIprnBH/XhDmyfDMfWM7fRKGJWxpMOho69N rSxJrWi9muASMZbiyhQ5E/ScR94sriZkzm+miCgJ0lan+ZBaVBtG4kgXy4iqAVyYwNHh ADzg==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mail-followup-to: Zsh hackers list <zsh-workers@xxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
Some odd behaviour:
$ echo *(n)
a3 a10 a-2
$ rm a10
$ echo *(n)
a-2 a3
(order of a-2 and a3 reversed after a10 is removed)
$ echo $LANG
en_GB.UTF-8
$ /lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.23-0ubuntu7) stable release version 2.23, by Roland McGrath et al.
I think the problem is that here, zsh uses a non-total order. We have:
a3 < a10 (same prefix, so numeric comparison of 3 < 10)
a10 < a-2 (as per strcoll(). - ignored in first pass. No same prefix, so no numeric comparison)
a-2 < a3 (as per strcoll(). - ignored in first pass. No same prefix, so no numeric comparison)
We've got a circle here. AFAIK, the behaviour is unspecified if
qsort() is called with a comparison function that doesn't
implement a total order, so the result could be random.
Once we remove a10, we're OK.
GNU sort -V and ls -v seem to "handle" the issue by giving up on
strcoll() which is not a lot better:
$ ls
a0 á0 a10 a-2 a3 b0
$ ls -v
a0 a3 a10 a-2 b0 á0
$ ls | sort
a0
á0
a10
a-2
a3
b0
$ ls | sort -V
a0
a3
a10
a-2
b0
á0
Maybe a better approach would be to break down the strings
between non-numeric and numeric parts and use strcoll() on the
non-numeric and number comparison on the numeric parts, stopping
at the first difference.
a3 < a-2 because a < a- as per strcoll (even though a3 > 1-2)
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author