lexical_sort

Trait StringSort

Source
pub trait StringSort {
    // Required methods
    fn string_sort(&mut self, cmp: impl FnMut(&str, &str) -> Ordering);
    fn string_sort_unstable(&mut self, cmp: impl FnMut(&str, &str) -> Ordering);
    fn string_sort_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
       where Cmp: FnMut(&str, &str) -> Ordering,
             Map: FnMut(&str) -> &str;
    fn string_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 strings. This is a convenient wrapper for the standard library sort functions.

This trait is implemented for all slices whose inner type implements AsRef<str>.

§Example

use lexical_sort::StringSort;

let slice = &mut ["Hello", " world", "!"];
slice.string_sort_unstable(lexical_sort::natural_lexical_cmp);

// or trim the strings before comparing:
slice.string_sort_unstable_by(lexical_sort::natural_lexical_cmp, str::trim_start);

If you want to sort file paths or OsStrings, use the PathSort trait instead.

Required Methods§

Source

fn string_sort(&mut self, cmp: 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 string_sort_unstable instead.

§Example
use lexical_sort::StringSort;

let slice = &mut ["Lorem", "ipsum", "dolor", "sit", "amet"];
slice.string_sort(lexical_sort::natural_lexical_cmp);

assert_eq!(slice, &["amet", "dolor", "ipsum", "Lorem", "sit"]);
Source

fn string_sort_unstable(&mut self, cmp: 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::StringSort;

let slice = &mut ["The", "quick", "brown", "fox"];
slice.string_sort_unstable(lexical_sort::natural_lexical_cmp);

assert_eq!(slice, &["brown", "fox", "quick", "The"]);
Source

fn string_sort_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
where Cmp: FnMut(&str, &str) -> Ordering, Map: FnMut(&str) -> &str,

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.

This is a stable sort, which is often not required. You can use string_sort_unstable instead.

§Example
use lexical_sort::StringSort;

let slice = &mut ["Eeny", " meeny", " miny", " moe"];
slice.string_sort_by(lexical_sort::natural_lexical_cmp, str::trim_start);

assert_eq!(slice, &["Eeny", " meeny", " miny", " moe"]);
Source

fn string_sort_unstable_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
where Cmp: FnMut(&str, &str) -> Ordering, Map: FnMut(&str) -> &str,

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.

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::StringSort;

let slice = &mut ["Eeny", " meeny", " miny", " moe"];
slice.string_sort_unstable_by(lexical_sort::natural_lexical_cmp, str::trim_start);

assert_eq!(slice, &["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.

Implementations on Foreign Types§

Source§

impl<A: AsRef<str>> StringSort for [A]

Source§

fn string_sort(&mut self, cmp: impl FnMut(&str, &str) -> Ordering)

Source§

fn string_sort_unstable(&mut self, cmp: impl FnMut(&str, &str) -> Ordering)

Source§

fn string_sort_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
where Cmp: FnMut(&str, &str) -> Ordering, Map: FnMut(&str) -> &str,

Source§

fn string_sort_unstable_by<Cmp, Map>(&mut self, cmp: Cmp, map: Map)
where Cmp: FnMut(&str, &str) -> Ordering, Map: FnMut(&str) -> &str,

Implementors§