Sort case-insensitively

This commit is contained in:
Ben S 2014-06-01 13:07:45 +01:00
parent 4722c8991b
commit a631bc099f
2 changed files with 9 additions and 6 deletions

View File

@ -4,6 +4,7 @@ use file::File;
use std::cmp::lexical_ordering; use std::cmp::lexical_ordering;
use column::{Column, Permissions, FileName, FileSize, User, Group}; use column::{Column, Permissions, FileName, FileSize, User, Group};
use unix::get_current_user_id; use unix::get_current_user_id;
use std::ascii::StrAsciiExt;
pub enum SortField { pub enum SortField {
Name, Extension, Size Name, Extension, Size
@ -83,8 +84,8 @@ impl Options {
Name => files.sort_by(|a, b| a.parts.cmp(&b.parts)), Name => files.sort_by(|a, b| a.parts.cmp(&b.parts)),
Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)), Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
Extension => files.sort_by(|a, b| { Extension => files.sort_by(|a, b| {
let exts = a.ext.cmp(&b.ext); let exts = a.ext.map(|e| e.to_ascii_lower()).cmp(&b.ext.map(|e| e.to_ascii_lower()));
let names = a.name.cmp(&b.name); let names = a.name.to_ascii_lower().cmp(&b.name.to_ascii_lower());
lexical_ordering(exts, names) lexical_ordering(exts, names)
}), }),
} }

10
sort.rs
View File

@ -1,3 +1,5 @@
use std::ascii::StrAsciiExt;
// This is an implementation of "natural sort order". See // This is an implementation of "natural sort order". See
// http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ // http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
// for more information and examples. It tries to sort "9" before // for more information and examples. It tries to sort "9" before
@ -9,16 +11,16 @@
#[deriving(Eq, Ord, TotalEq, TotalOrd)] #[deriving(Eq, Ord, TotalEq, TotalOrd)]
pub enum SortPart<'a> { pub enum SortPart<'a> {
Stringular(&'a str), Numeric(u64),
Numeric(u32), Stringular(String),
} }
impl<'a> SortPart<'a> { impl<'a> SortPart<'a> {
pub fn from_string(is_digit: bool, slice: &'a str) -> SortPart<'a> { pub fn from_string(is_digit: bool, slice: &'a str) -> SortPart<'a> {
if is_digit { if is_digit {
Numeric(from_str::<u32>(slice).unwrap()) Numeric(from_str::<u64>(slice).expect(slice.to_owned()))
} else { } else {
Stringular(slice) Stringular(slice.to_ascii_lower())
} }
} }