zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Surprising behaviour with numeric glob sort
Date: Wed, 31 May 2017 22:24:53 +0100	[thread overview]
Message-ID: <20170531212453.GA31563@chaz.gmail.com> (raw)

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


             reply	other threads:[~2017-05-31 21:25 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-31 21:24 Stephane Chazelas [this message]
2017-06-01 22:29 ` Bart Schaefer
2017-06-02  9:03   ` Stephane Chazelas
2017-06-02 23:19     ` Bart Schaefer
2017-06-03 21:16       ` Stephane Chazelas
2017-06-04  0:07         ` Bart Schaefer
2017-06-04 17:31           ` Stephane Chazelas
2017-06-04 22:01             ` Bart Schaefer
2017-06-05 11:54               ` Stephane Chazelas
2017-06-05 19:15                 ` Stephane Chazelas
2017-06-06  3:13                 ` Bart Schaefer
2017-06-06  9:22                   ` Stephane Chazelas
2017-06-07  8:41                 ` Stephane Chazelas
2017-06-17 18:11                   ` Bart Schaefer
2017-06-06 14:44         ` Vincent Lefevre
2017-06-06 16:47           ` Stephane Chazelas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170531212453.GA31563@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).