Skip to content

Commit

Permalink
fixup! Create closure to do sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldhsu committed Jan 8, 2020
1 parent a651851 commit bab0f0e
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/sort.rs
Expand Up @@ -5,33 +5,31 @@ use std::cmp::Ordering;
pub type Sorter = Box<dyn Fn(&Meta, &Meta) -> Ordering>;

pub fn create_sorter(flags: &Flags) -> Sorter {
let mut sorters: Vec<(bool, Sorter)> = vec![];
let mut sorters: Vec<(SortOrder, Sorter)> = vec![];
match flags.directory_order {
DirOrderFlag::First => {
sorters.push((false, Box::new(with_dirs_first)));
sorters.push((SortOrder::Default, Box::new(with_dirs_first)));
}
DirOrderFlag::Last => {
sorters.push((true, Box::new(with_dirs_first)));
sorters.push((SortOrder::Reverse, Box::new(with_dirs_first)));
}
DirOrderFlag::None => {}
};
let reverse = flags.sort_order == SortOrder::Reverse;
let other_sort = match flags.sort_by {
SortFlag::Name => by_name,
SortFlag::Size => by_size,
SortFlag::Time => by_date,
};
sorters.push((reverse, Box::new(other_sort)));
sorters.push((flags.sort_order, Box::new(other_sort)));

Box::new(move |a, b| {
for (reverse, sorter) in sorters.iter() {
for (direction, sorter) in sorters.iter() {
match (sorter)(a, b) {
Ordering::Equal => continue,
ordering => {
if *reverse {
return ordering.reverse();
} else {
return ordering;
return match direction {
SortOrder::Reverse => ordering.reverse(),
SortOrder::Default => ordering,
}
}
}
Expand Down

0 comments on commit bab0f0e

Please sign in to comment.