Skip to content

Commit

Permalink
make name ordering case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
ahti committed Aug 21, 2020
1 parent d8196e7 commit cdc10d5
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,25 @@ pub fn directory_listing(
}
}

if let Some(sorting_method) = query_params.sort {
match sorting_method {
SortingMethod::Name => entries
.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone())),
SortingMethod::Size => entries.sort_by(|e1, e2| {
// If we can't get the size of the entry (directory for instance)
// let's consider it's 0b
e2.size
.unwrap_or_else(|| ByteSize::b(0))
.cmp(&e1.size.unwrap_or_else(|| ByteSize::b(0)))
}),
SortingMethod::Date => entries.sort_by(|e1, e2| {
// If, for some reason, we can't get the last modification date of an entry
// let's consider it was modified on UNIX_EPOCH (01/01/19270 00:00:00)
e2.last_modification_date
.unwrap_or(SystemTime::UNIX_EPOCH)
.cmp(&e1.last_modification_date.unwrap_or(SystemTime::UNIX_EPOCH))
}),
};
} else {
// Sort in alphanumeric order by default
entries.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone()))
}
match query_params.sort.unwrap_or(SortingMethod::Name) {
SortingMethod::Name => entries.sort_by(|e1, e2| {
alphanumeric_sort::compare_str(e1.name.to_lowercase(), e2.name.to_lowercase())
}),
SortingMethod::Size => entries.sort_by(|e1, e2| {
// If we can't get the size of the entry (directory for instance)
// let's consider it's 0b
e2.size
.unwrap_or_else(|| ByteSize::b(0))
.cmp(&e1.size.unwrap_or_else(|| ByteSize::b(0)))
}),
SortingMethod::Date => entries.sort_by(|e1, e2| {
// If, for some reason, we can't get the last modification date of an entry
// let's consider it was modified on UNIX_EPOCH (01/01/19270 00:00:00)
e2.last_modification_date
.unwrap_or(SystemTime::UNIX_EPOCH)
.cmp(&e1.last_modification_date.unwrap_or(SystemTime::UNIX_EPOCH))
}),
};

if let Some(sorting_order) = query_params.order {
if let SortingOrder::Descending = sorting_order {
Expand Down

0 comments on commit cdc10d5

Please sign in to comment.