summaryrefslogtreecommitdiffstats
path: root/cli/src/main.rs
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-08-01 01:41:30 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-08-01 01:41:30 +0200
commit29aaafeb67df0c5d9382c2cb9e9c3d1a07afba09 (patch)
treea9e9edea88bf3d89af307ca838be06e541fecea7 /cli/src/main.rs
parent58fff5695603443c0b5a9b82dba3ac6a92819219 (diff)
downloadmoney-29aaafeb67df0c5d9382c2cb9e9c3d1a07afba09.tar.gz
tighter multiple sorting
Why write much code when less code to trick?
Diffstat (limited to 'cli/src/main.rs')
-rw-r--r--cli/src/main.rs40
1 files changed, 9 insertions, 31 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs
index 3ffbe88..4743ea0 100644
--- a/cli/src/main.rs
+++ b/cli/src/main.rs
@@ -143,7 +143,15 @@ fn main() {
transactions.sort_by_key(|t| t.date);
} else {
let mut sorts: Vec<_> = sort.iter().map(sort_by_func).collect();
- inner_sort_by(&mut transactions, &mut sorts);
+ transactions.sort_by(|t1, t2| {
+ for sort in &mut sorts {
+ let sort = sort(t1, t2);
+ if sort != Ordering::Equal {
+ return sort;
+ }
+ }
+ return Ordering::Equal;
+ });
}
println!("{}", Table::new(transactions).with(Style::psql()));
}
@@ -156,33 +164,3 @@ fn sort_by_func(sort: &SortTarget) -> impl FnMut(&&Transaction, &&Transaction) -
SortTarget::Date => |t1: &&Transaction, t2: &&Transaction| t1.date.cmp(&t2.date),
}
}
-
-fn inner_sort_by<T, F>(v: &mut [T], cmps: &mut [F])
-where
- F: FnMut(&T, &T) -> Ordering,
-{
- 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.
- for i in 0..v.len() {
- if cmps[0](&v[i], &v[lower]) != Ordering::Equal {
- ranges.push(lower..i);
- lower = i;
- }
- }
-
- // 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..]);
- }
- }
-}