summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cli/src/main.rs9
1 files changed, 6 insertions, 3 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs
index e405e48..3ffbe88 100644
--- a/cli/src/main.rs
+++ b/cli/src/main.rs
@@ -161,15 +161,16 @@ fn inner_sort_by<T, F>(v: &mut [T], cmps: &mut [F])
where
F: FnMut(&T, &T) -> Ordering,
{
- // Early out
- if v.len() < 2 || cmps.is_empty() {
+ if v.is_empty() || cmps.is_empty() {
return;
}
+ // Sort the slice using the first comparison.
v.sort_by(&mut cmps[0]);
+ // Find ranges of consecutive equal items according to the first comparison.
let mut ranges = Vec::new();
- let mut lower = 0; // Lower bound of current equal range
+ let mut lower = 0; // Lower bound of current equal range.
for i in 0..v.len() {
if cmps[0](&v[i], &v[lower]) != Ordering::Equal {
ranges.push(lower..i);
@@ -177,6 +178,8 @@ where
}
}
+ // If we still have comparisons to use, sort the ranges of consecutive items recursively.
+ // The recursion ensures that ranges of equal items aren't mixed between comparisons further down.
if cmps.len() != 1 {
for range in ranges {
inner_sort_by(&mut v[range], &mut cmps[1..]);