pub trait PathSort {
// Required methods
fn path_sort(&mut self, comparator: impl FnMut(&str, &str) -> Ordering);
fn path_sort_unstable(
&mut self,
comparator: impl FnMut(&str, &str) -> Ordering,
);
fn path_sort_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
where Cmp: FnMut(&str, &str) -> Ordering,
Map: FnMut(&str) -> &str;
fn path_sort_unstable_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
where Cmp: FnMut(&str, &str) -> Ordering,
Map: FnMut(&str) -> &str;
}
Expand description
A trait to sort paths and OsStrings. This is a convenient wrapper for the standard library sort functions.
This trait is implemented for all slices whose inner type implements AsRef<Path>
.
§Example
use lexical_sort::PathSort;
let slice: &mut [&Path] = &mut ["Hello".as_ref(), " world".as_ref(), "!".as_ref()];
slice.path_sort_unstable(lexical_sort::natural_lexical_cmp);
// or trim the strings before comparing:
slice.path_sort_unstable_by(lexical_sort::natural_lexical_cmp, str::trim_start);
If you want to sort regular strings, use the StringSort
trait instead.
Required Methods§
Sourcefn path_sort(&mut self, comparator: impl FnMut(&str, &str) -> Ordering)
fn path_sort(&mut self, comparator: impl FnMut(&str, &str) -> Ordering)
Sorts the items using the provided comparison function.
This is a stable sort, which is often not required.
You can use path_sort_unstable
instead.
§Example
use lexical_sort::PathSort;
let mut vec: Vec<&Path> = paths(&["Lorem", "ipsum", "dolor", "sit", "amet"]);
vec.path_sort(lexical_sort::natural_lexical_cmp);
assert_eq!(vec, paths(&["amet", "dolor", "ipsum", "Lorem", "sit"]));
Sourcefn path_sort_unstable(&mut self, comparator: impl FnMut(&str, &str) -> Ordering)
fn path_sort_unstable(&mut self, comparator: impl FnMut(&str, &str) -> Ordering)
Sorts the items using the provided comparison function.
This sort is unstable: The original order of equal strings is not preserved. It is slightly more efficient than the stable alternative.
§Example
use lexical_sort::PathSort;
let mut vec: Vec<&Path> = paths(&["The", "quick", "brown", "fox"]);
vec.path_sort_unstable(lexical_sort::natural_lexical_cmp);
assert_eq!(vec, paths(&["brown", "fox", "quick", "The"]));
Sourcefn path_sort_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
fn path_sort_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
Sorts the items using the provided comparison function and another function that is applied to each string before the comparison. This can be used to trim the strings.
If you do anything more complicated than trimming, you’ll likely run into lifetime problems.
In this case you should use [_]::sort_by()
directly. You’ll need to call
to_string_lossy()
or to_str().unwrap()
to convert a Path
or OsStr
to a &str
first.
This is a stable sort, which is often not required.
You can use path_sort_unstable
instead.
§Example
use lexical_sort::PathSort;
let mut vec: Vec<&Path> = paths(&["Eeny", " meeny", " miny", " moe"]);
vec.path_sort_by(lexical_sort::natural_lexical_cmp, str::trim_start);
assert_eq!(vec, paths(&["Eeny", " meeny", " miny", " moe"]));
Sourcefn path_sort_unstable_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
fn path_sort_unstable_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
Sorts the items using the provided comparison function and another function that is applied to each string before the comparison. This can be used to trim the strings.
If you do anything more complicated than trimming, you’ll likely run into lifetime problems.
In this case you should use [_]::sort_by()
directly. You’ll need to call
to_string_lossy()
or to_str().unwrap()
to convert a Path
or OsStr
to a &str
first.
This sort is unstable: The original order of equal strings is not preserved. It is slightly more efficient than the stable alternative.
§Example
use lexical_sort::PathSort;
let mut vec: Vec<&Path> = paths(&["Eeny", " meeny", " miny", " moe"]);
vec.path_sort_by(lexical_sort::natural_lexical_cmp, str::trim_start);
assert_eq!(vec, paths(&["Eeny", " meeny", " miny", " moe"]));
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.