diff --git a/yazi-config/preset/keymap.toml b/yazi-config/preset/keymap.toml index 31bdb5b6..93063d4b 100644 --- a/yazi-config/preset/keymap.toml +++ b/yazi-config/preset/keymap.toml @@ -96,12 +96,14 @@ keymap = [ { on = [ "N" ], exec = "find_arrow --previous" }, # Sorting - { on = [ ",", "a" ], exec = "sort alphabetical --dir_first", desc = "Sort alphabetically" }, - { on = [ ",", "A" ], exec = "sort alphabetical --reverse --dir_first", desc = "Sort alphabetically (reverse)" }, - { on = [ ",", "c" ], exec = "sort created --dir_first", desc = "Sort by creation time" }, - { on = [ ",", "C" ], exec = "sort created --reverse --dir_first", desc = "Sort by creation time (reverse)" }, { on = [ ",", "m" ], exec = "sort modified --dir_first", desc = "Sort by modified time" }, { on = [ ",", "M" ], exec = "sort modified --reverse --dir_first", desc = "Sort by modified time (reverse)" }, + { on = [ ",", "c" ], exec = "sort created --dir_first", desc = "Sort by created time" }, + { on = [ ",", "C" ], exec = "sort created --reverse --dir_first", desc = "Sort by created time (reverse)" }, + { on = [ ",", "e" ], exec = "sort extension --dir_first", desc = "Sort by extension" }, + { on = [ ",", "E" ], exec = "sort extension --reverse --dir_first", desc = "Sort by extension (reverse)" }, + { on = [ ",", "a" ], exec = "sort alphabetical --dir_first", desc = "Sort alphabetically" }, + { on = [ ",", "A" ], exec = "sort alphabetical --reverse --dir_first", desc = "Sort alphabetically (reverse)" }, { on = [ ",", "n" ], exec = "sort natural --dir_first", desc = "Sort naturally" }, { on = [ ",", "N" ], exec = "sort natural --reverse --dir_first", desc = "Sort naturally (reverse)" }, { on = [ ",", "s" ], exec = "sort size --dir_first", desc = "Sort by size" }, diff --git a/yazi-config/src/manager/sorting.rs b/yazi-config/src/manager/sorting.rs index 41dc29f8..1656c5ee 100644 --- a/yazi-config/src/manager/sorting.rs +++ b/yazi-config/src/manager/sorting.rs @@ -8,9 +8,10 @@ use serde::{Deserialize, Serialize}; pub enum SortBy { #[default] None, - Alphabetical, - Created, Modified, + Created, + Extension, + Alphabetical, Natural, Size, } @@ -21,9 +22,10 @@ impl FromStr for SortBy { fn from_str(s: &str) -> Result { Ok(match s { "none" => Self::None, - "alphabetical" => Self::Alphabetical, - "created" => Self::Created, "modified" => Self::Modified, + "created" => Self::Created, + "extension" => Self::Extension, + "alphabetical" => Self::Alphabetical, "natural" => Self::Natural, "size" => Self::Size, _ => bail!("invalid sort_by value: {s}"), @@ -41,9 +43,10 @@ impl ToString for SortBy { fn to_string(&self) -> String { match self { Self::None => "none", - Self::Alphabetical => "alphabetical", - Self::Created => "created", Self::Modified => "modified", + Self::Created => "created", + Self::Extension => "extension", + Self::Alphabetical => "alphabetical", Self::Natural => "natural", Self::Size => "size", } diff --git a/yazi-core/src/files/sorter.rs b/yazi-core/src/files/sorter.rs index 9eca8551..840896e7 100644 --- a/yazi-core/src/files/sorter.rs +++ b/yazi-core/src/files/sorter.rs @@ -31,15 +31,27 @@ impl FilesSorter { match self.by { SortBy::None => return false, - SortBy::Alphabetical => items.sort_unstable_by(by_alphabetical), - SortBy::Created => items.sort_unstable_by(|a, b| { - let ord = self.cmp(a.created, b.created, self.promote(a, b)); - if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } - }), SortBy::Modified => items.sort_unstable_by(|a, b| { let ord = self.cmp(a.modified, b.modified, self.promote(a, b)); if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } }), + SortBy::Created => items.sort_unstable_by(|a, b| { + let ord = self.cmp(a.created, b.created, self.promote(a, b)); + if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } + }), + SortBy::Extension => items.sort_unstable_by(|a, b| { + let ord = if self.sensitive { + self.cmp(a.url.extension(), b.url.extension(), self.promote(a, b)) + } else { + self.cmp( + a.url.extension().map(|s| s.to_ascii_lowercase()), + b.url.extension().map(|s| s.to_ascii_lowercase()), + self.promote(a, b), + ) + }; + if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } + }), + SortBy::Alphabetical => items.sort_unstable_by(by_alphabetical), SortBy::Natural => self.sort_naturally(items), SortBy::Size => items.sort_unstable_by(|a, b| { let aa = if a.is_dir() { sizes.get(&a.url).copied() } else { None };