Skip to content

Commit

Permalink
Fix list sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
lusingander committed Jan 3, 2025
1 parent dc8a6d2 commit b4e954d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
23 changes: 14 additions & 9 deletions src/pages/bucket_list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cmp::Ordering, rc::Rc};
use std::rc::Rc;

use laurier::{highlight::highlight_matched_text, key_code, key_code_char};
use ratatui::{
Expand Down Expand Up @@ -415,14 +415,19 @@ impl BucketListPage {
let items = &self.bucket_items;
let selected = self.sort_dialog_state.selected();

#[allow(clippy::type_complexity)]
let sort_func: Box<dyn FnMut(&usize, &usize) -> Ordering> = match selected {
BucketListSortType::Default => Box::new(|a, b| a.cmp(b)),
BucketListSortType::NameAsc => Box::new(|a, b| items[*a].name.cmp(&items[*b].name)),
BucketListSortType::NameDesc => Box::new(|a, b| items[*b].name.cmp(&items[*a].name)),
};

self.view_indices.sort_by(sort_func);
match selected {
BucketListSortType::Default => {
self.view_indices.sort();
}
BucketListSortType::NameAsc => {
self.view_indices
.sort_by(|a, b| items[*a].name.cmp(&items[*b].name));
}
BucketListSortType::NameDesc => {
self.view_indices
.sort_by(|a, b| items[*b].name.cmp(&items[*a].name));
}
}
}

pub fn current_selected_item(&self) -> &BucketItem {
Expand Down
35 changes: 22 additions & 13 deletions src/pages/object_list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cmp::Ordering, rc::Rc};
use std::rc::Rc;

use chrono::{DateTime, Local};
use laurier::{highlight::highlight_matched_text, key_code, key_code_char};
Expand Down Expand Up @@ -442,26 +442,35 @@ impl ObjectListPage {
let items = &self.object_items;
let selected = self.sort_dialog_state.selected();

#[allow(clippy::type_complexity)]
let sort_func: Box<dyn FnMut(&usize, &usize) -> Ordering> = match selected {
ObjectListSortType::Default => Box::new(|a, b| a.cmp(b)),
ObjectListSortType::NameAsc => Box::new(|a, b| items[*a].name().cmp(items[*b].name())),
ObjectListSortType::NameDesc => Box::new(|a, b| items[*b].name().cmp(items[*a].name())),
match selected {
ObjectListSortType::Default => {
self.view_indices.sort();
}
ObjectListSortType::NameAsc => {
self.view_indices
.sort_by(|a, b| items[*a].name().cmp(items[*b].name()));
}
ObjectListSortType::NameDesc => {
self.view_indices
.sort_by(|a, b| items[*b].name().cmp(items[*a].name()));
}
ObjectListSortType::LastModifiedAsc => {
Box::new(|a, b| items[*a].last_modified().cmp(&items[*b].last_modified()))
self.view_indices
.sort_by(|a, b| items[*a].last_modified().cmp(&items[*b].last_modified()));
}
ObjectListSortType::LastModifiedDesc => {
Box::new(|a, b| items[*b].last_modified().cmp(&items[*a].last_modified()))
self.view_indices
.sort_by(|a, b| items[*b].last_modified().cmp(&items[*a].last_modified()));
}
ObjectListSortType::SizeAsc => {
Box::new(|a, b| items[*a].size_byte().cmp(&items[*b].size_byte()))
self.view_indices
.sort_by(|a, b| items[*a].size_byte().cmp(&items[*b].size_byte()));
}
ObjectListSortType::SizeDesc => {
Box::new(|a, b| items[*b].size_byte().cmp(&items[*a].size_byte()))
self.view_indices
.sort_by(|a, b| items[*b].size_byte().cmp(&items[*a].size_byte()));
}
};

self.view_indices.sort_by(sort_func);
}
}

pub fn current_selected_item(&self) -> &ObjectItem {
Expand Down

0 comments on commit b4e954d

Please sign in to comment.