[][src]Trait lexical_sort::StringSort

pub trait StringSort {
    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
; }

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

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"]);

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"]);

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"]);

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"]);
Loading content...

Implementations on Foreign Types

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

Loading content...

Implementors

Loading content...