Zsh Mailing List Archive
Messages sorted by:
Re: Surprising behaviour with numeric glob sort
- X-seq: zsh-workers 41202
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: Surprising behaviour with numeric glob sort
- Date: Fri, 2 Jun 2017 16:19:05 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version; bh=Wm0ZL3Y6xQmKiGBCwdT5vyw4ytrvRNoq9gqpuzGM++s=; b=woa6QyQlQWoRN89QrCJh4z3ryLDAiHHlunOFAWuKPkmXFKuWVRC/WS6T9x9bC7t/Vf dO5gfp6k14CLSPPFhWfiRCOddCpGlwBXseukY3SLrhxBKo3bYn0/yt6GGUS6D2AfyCWw 8+0Rm+NhVYJZzYkJc2dSvc+XTLPF6Ms12uythqJVwOORZbMu/83X276jmj0h1x9JL79H AngkBCfm0PQM22+2cUjI9TKRUFMqabJ2yy+gIud8PhBqy7UK4KkxIxlEJa+SZzmKCtxH +gu0qOkIuCuzpWL4nyajw5KNUkuMtZY8Yjx/JNMhgXzKoP5g8T8eCqfT2NWXQs9OP9hY mfgw==
- In-reply-to: <20170602090332.GA6574@chaz.gmail.com>
- List-help: <mailto:firstname.lastname@example.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:email@example.com>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <20170531212453.GA31563@chaz.gmail.com> <170601152943.ZM4783@torch.brasslantern.com> <20170602090332.GA6574@chaz.gmail.com>
On Jun 2, 10:03am, Stephane Chazelas wrote:
} $ echo *(n)
} zsh-10 zsh2 zsh10 zsh-3
} (here in my en_GB.UTF-8 GNU locale)
} is unexpected/broken. "zsh" sorts before "zsh-" in my locale, so
} I'd expect the zsh2, zsh10 to come before zsh-3, zsh-10 which is
} the basis of my proposal. In any case, zsh-3 should come before
} zsh-10, nobody can argue against that.
Well, one could argue that "-10" should be treated as negative ten
and therefore should sort before negative three, but I'm not sure
we want to get into that.
The reason you get the result above is of course because most sort
algorithms assume there is a total order and therefore that it is
not necessary to compare every possible pairing of elements.
Your proposal was
> } break down the strings
> } between non-numeric and numeric parts and use strcoll() on the
> } non-numeric and number comparison on the numeric parts
As far as I can tell that's exactly zstrbcmp in zle_tricky.c does.
zstrcmp in sort.c on the other hand first attempts strcoll and
only compares numeric parts if it can find corresponding numeric
substrings in both input strings. That is, "zsh-3" is never
compared numerically to "zsh2" because "zsh2" and "zsh-" are
considered already to differ.
In either case, if zstrcmp or zstrbcomp find a digit, they consume
more digits until they hit a non-digit or two not-equal digits, and
then look both backward and forward for digits to calculate the
numeric value for comparison.
So I think what you propose is that when "zsh1" is found to have a
difference with "zsh-", the algorithm should look forward across
"zsh-" to find "3" and at that point end up comparing "10" to "3"?
That would lead to the order in your example becoming
zsh2 zsh-3 zsh10 zsh-10.
However, that would also mean that in strings with different sets
of numeric substrings the numeric comparisons might be be "detected"
after different prefixes for different pairs of strings; I think
the result there might be even more confusing, but I can't come up
with a specific example.
It also means having to copy non-numeric substrings during every
comparison, so as to be able to call strcoll without modifying the
input strings. (What's the alternative?) This would probably make
sorting prohibitively slow.
Messages sorted by: