diff options
Diffstat (limited to 'cli/src/main.rs')
| -rw-r--r-- | cli/src/main.rs | 40 |
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..]); - } - } -} |
