coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

Inconsistent sort behavior

Open odysei opened this issue 7 months ago • 3 comments

While doing some sorting with new sort (v 0.8.0) that came with Ubuntu 26.04, I have noticed an inconsistent behavior. Attaching example sort1.txt and sort2.txt files that:

  • lines 6&7 got randomly assigned using sort;
  • stay invariant if you run over sort (again).

Expected behavior:

  1. files sort1.txt and sort2 are identical;
  2. sort produces the same output for inputs from sort1.txt or sort2.txt

sort1.txt sort2.txt

odysei avatar Apr 24 '26 13:04 odysei

I am currently looking into this, I currently worked on a minimal example, that shows that this is indeed an issue. Two files containing:

01
0_1
02

and

0_1
01
02

will produce:

01
0_1
0_1
01
02
02

Looking into the compare code, when setting the global_settings.precomputed.fast_lexicographic = true; it will produce:

01
01
02
02
0_1
0_1

Using --sort=<general-numeric,human-numeric> also produces a well sorted list.

ksgk1 avatar Apr 25 '26 12:04 ksgk1

I created a PR to fix this issue. https://github.com/uutils/coreutils/pull/12013

ksgk1 avatar Apr 26 '26 11:04 ksgk1

@sylvestre This bug is quite severe. I imagine it will need patching in Ubuntu.

The issue is all the programs with localization implemented (e.g. sort, join, expr) use AlternateHandling::Shifted. This leads to some obviously incorrect behavior, for example:

$ expr 'USA' = 'U.S.A.'
0
$ uu_expr 'USA' = 'U.S.A.'
1
$ printf '_\n.\n\n' | sort -u

.
_
$ printf '_\n.\n\n' | uu_sort -u
_

@pixelb FYI, I'll have a look at adding some tests with punctuation later.

collinfunk avatar Apr 28 '26 15:04 collinfunk