diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-07-31 22:58:39 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-07-31 22:58:39 +0200 |
| commit | 77f1c55974df0e3e9f088eb8972dae83cbae70cc (patch) | |
| tree | ba8dfa18b58def6528720e23158f6fe4264e3f50 | |
| parent | abce6f2ae0f5fa36ea2069e69a4a070156644ea1 (diff) | |
| download | money-77f1c55974df0e3e9f088eb8972dae83cbae70cc.tar.gz | |
inner_sort_by: comments
| -rw-r--r-- | cli/src/main.rs | 9 |
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..]); |
