From 60df3b473ca183d2efba1f9341986b267563ba08 Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 6 Sep 2023 23:25:04 +0800 Subject: [PATCH] fix: sort path ignore case (#264) --- assets/index.js | 10 ++++----- src/server.rs | 58 +++++++++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/assets/index.js b/assets/index.js index 3e73e01..7a897b6 100644 --- a/assets/index.js +++ b/assets/index.js @@ -316,13 +316,13 @@ function renderPathsTableHead() { ${headerItems.map(item => { let svg = ``; - let order = "asc"; + let order = "desc"; if (PARAMS.sort === item.name) { - if (PARAMS.order === "asc") { - order = "desc"; - svg = `` - } else { + if (PARAMS.order === "desc") { + order = "asc"; svg = `` + } else { + svg = `` } } const qs = new URLSearchParams({ ...PARAMS, order, sort: item.name }).toString(); diff --git a/src/server.rs b/src/server.rs index fea590d..ec2843f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -952,28 +952,11 @@ impl Server { ) -> Result<()> { if let Some(sort) = query_params.get("sort") { if sort == "name" { - paths.sort_by(|v1, v2| match v1.path_type.cmp(&v2.path_type) { - Ordering::Equal => { - alphanumeric_sort::compare_str(v1.name.clone(), v2.name.clone()) - } - v => v, - }) + paths.sort_by(|v1, v2| v1.sort_by_name(v2)) } else if sort == "mtime" { - paths.sort_by(|v1, v2| match v1.path_type.cmp(&v2.path_type) { - Ordering::Equal => v1.mtime.cmp(&v2.mtime), - v => v, - }) + paths.sort_by(|v1, v2| v1.sort_by_mtime(v2)) } else if sort == "size" { - paths.sort_by(|v1, v2| match v1.path_type.cmp(&v2.path_type) { - Ordering::Equal => { - if v1.is_dir() { - alphanumeric_sort::compare_str(v1.name.clone(), v2.name.clone()) - } else { - v1.size.unwrap_or(0).cmp(&v2.size.unwrap_or(0)) - } - } - v => v, - }) + paths.sort_by(|v1, v2| v1.sort_by_size(v2)) } if query_params .get("order") @@ -983,7 +966,7 @@ impl Server { paths.reverse() } } else { - paths.sort_unstable(); + paths.sort_by(|v1, v2| v1.sort_by_name(v2)) } if query_params.contains_key("simple") { let output = paths @@ -1286,9 +1269,42 @@ impl PathItem { ), } } + pub fn base_name(&self) -> &str { self.name.split('/').last().unwrap_or_default() } + + pub fn sort_by_name(&self, other: &Self) -> Ordering { + match self.path_type.cmp(&other.path_type) { + Ordering::Equal => { + alphanumeric_sort::compare_str(self.name.to_lowercase(), other.name.to_lowercase()) + } + v => v, + } + } + + pub fn sort_by_mtime(&self, other: &Self) -> Ordering { + match self.path_type.cmp(&other.path_type) { + Ordering::Equal => self.mtime.cmp(&other.mtime), + v => v, + } + } + + pub fn sort_by_size(&self, other: &Self) -> Ordering { + match self.path_type.cmp(&other.path_type) { + Ordering::Equal => { + if self.is_dir() { + alphanumeric_sort::compare_str( + self.name.to_lowercase(), + other.name.to_lowercase(), + ) + } else { + self.size.unwrap_or(0).cmp(&other.size.unwrap_or(0)) + } + } + v => v, + } + } } #[derive(Debug, Serialize, Eq, PartialEq)]