From 3564dc57924acd958a77085836dd34f9fed0fd76 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Mon, 26 Jul 2021 16:27:24 +0200 Subject: [PATCH] sort: compare strings before comparing hashes Since lines that compare equal should be sorted together, we need to first compare the lines (taking settings into account). Only if they do not compare equal we should compare the hashes. --- src/uu/sort/src/sort.rs | 17 ++++++++++++++++- tests/by-util/test_sort.rs | 10 ++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 06bcc7ff8..aa4d483c0 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -1398,7 +1398,22 @@ fn compare_by<'a>( let settings = &selector.settings; let cmp: Ordering = match settings.mode { - SortMode::Random => random_shuffle(a_str, b_str, &global_settings.salt.unwrap()), + SortMode::Random => { + // check if the two strings are equal + if custom_str_cmp( + a_str, + b_str, + settings.ignore_non_printing, + settings.dictionary_order, + settings.ignore_case, + ) == Ordering::Equal + { + Ordering::Equal + } else { + // Only if they are not equal compare by the hash + random_shuffle(a_str, b_str, &global_settings.salt.unwrap()) + } + } SortMode::Numeric => { let a_num_info = &a_line_data.num_infos [a.index * global_settings.precomputed.num_infos_per_line + num_info_index]; diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 16437d526..8573ce03f 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -235,6 +235,16 @@ fn test_random_shuffle_two_runs_not_the_same() { } } +#[test] +fn test_random_ignore_case() { + let input = "ABC\nABc\nAbC\nAbc\naBC\naBc\nabC\nabc\n"; + new_ucmd!() + .args(&["-fR"]) + .pipe_in(input) + .succeeds() + .stdout_is(input); +} + #[test] fn test_numeric_floats_and_ints() { test_helper(