Skip to content

Commit b3d8706

Browse files
ksgk1cakebaker
andauthored
sort: fix inconsistent sort ordering under i18n-collator with equal sorting keys (#12013)
* sort: Fix inconsistent sort orderg under i18n-collator with equal sorting keys. * Test cases for fix #11980 * Simplyfing fix for #11980 * Fix clippy lint and rename test files. * Remove old test files * Update tests/by-util/test_sort.rs Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com> * Update tests/by-util/test_sort.rs Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com> * Removing redundant test and swapping default order for sort to match sort's ordering. * Comment for clarification. --------- Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
1 parent 3b2ff61 commit b3d8706

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/uu/sort/src/sort.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2655,7 +2655,13 @@ fn compare_by<'a>(
26552655
if global_settings.precomputed.fast_locale_collation {
26562656
let a_key = a_line_data.collation_key(a.index);
26572657
let b_key = b_line_data.collation_key(b.index);
2658-
let cmp = a_key.cmp(b_key);
2658+
let mut cmp = a_key.cmp(b_key);
2659+
// If collation keys are equal, fall back to lexicographic comparison
2660+
// This can be the case for inputs like `01` and `0_1`, which have equal keys
2661+
if cmp == Ordering::Equal {
2662+
// Reversing the order to match sort's sorting behaviour
2663+
cmp = b.line.cmp(a.line);
2664+
}
26592665
return if global_settings.reverse {
26602666
cmp.reverse()
26612667
} else {

tests/by-util/test_sort.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,4 +2960,25 @@ e f 5436 down data path1 path2 path3 path4 path5\n";
29602960
.stdout_is(input);
29612961
}
29622962

2963+
#[test]
2964+
fn test_consistent_sorting_with_i18n_collate() {
2965+
// Regression test for issue #11980
2966+
// Lexicographic fallback sorting for equal sorting keys for 01 and 0_1
2967+
let expected_output = "0_1\n0_1\n01\n01\n02\n02\n";
2968+
new_ucmd!()
2969+
.env("LC_ALL", "en_US.UTF-8")
2970+
.arg("fix_i18n_collate_inconsistency_1.txt")
2971+
.arg("fix_i18n_collate_inconsistency_2.txt")
2972+
.succeeds()
2973+
.stdout_is(expected_output);
2974+
2975+
let expected_output = "01\n01\n02\n02\n0_1\n0_1\n";
2976+
new_ucmd!()
2977+
.env("LC_ALL", "C")
2978+
.arg("fix_i18n_collate_inconsistency_1.txt")
2979+
.arg("fix_i18n_collate_inconsistency_2.txt")
2980+
.succeeds()
2981+
.stdout_is(expected_output);
2982+
}
2983+
29632984
/* spell-checker: enable */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
01
2+
0_1
3+
02
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0_1
2+
01
3+
02

0 commit comments

Comments
 (0)