From a631bc099fc84f4c466e237e5bf05fdce551f62f Mon Sep 17 00:00:00 2001 From: Ben S Date: Sun, 1 Jun 2014 13:07:45 +0100 Subject: [PATCH] Sort case-insensitively --- options.rs | 5 +++-- sort.rs | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/options.rs b/options.rs index 22636ba..8255732 100644 --- a/options.rs +++ b/options.rs @@ -4,6 +4,7 @@ use file::File; use std::cmp::lexical_ordering; use column::{Column, Permissions, FileName, FileSize, User, Group}; use unix::get_current_user_id; +use std::ascii::StrAsciiExt; pub enum SortField { Name, Extension, Size @@ -83,8 +84,8 @@ impl Options { Name => files.sort_by(|a, b| a.parts.cmp(&b.parts)), Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)), Extension => files.sort_by(|a, b| { - let exts = a.ext.cmp(&b.ext); - let names = a.name.cmp(&b.name); + let exts = a.ext.map(|e| e.to_ascii_lower()).cmp(&b.ext.map(|e| e.to_ascii_lower())); + let names = a.name.to_ascii_lower().cmp(&b.name.to_ascii_lower()); lexical_ordering(exts, names) }), } diff --git a/sort.rs b/sort.rs index 22c269e..3c76dbb 100644 --- a/sort.rs +++ b/sort.rs @@ -1,3 +1,5 @@ +use std::ascii::StrAsciiExt; + // This is an implementation of "natural sort order". See // http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ // for more information and examples. It tries to sort "9" before @@ -9,16 +11,16 @@ #[deriving(Eq, Ord, TotalEq, TotalOrd)] pub enum SortPart<'a> { - Stringular(&'a str), - Numeric(u32), + Numeric(u64), + Stringular(String), } impl<'a> SortPart<'a> { pub fn from_string(is_digit: bool, slice: &'a str) -> SortPart<'a> { if is_digit { - Numeric(from_str::(slice).unwrap()) + Numeric(from_str::(slice).expect(slice.to_owned())) } else { - Stringular(slice) + Stringular(slice.to_ascii_lower()) } }