Trait comparator::Comparator
source · [−]pub trait Comparator<T> where
T: ?Sized, {
fn compare(&self, a: &T, b: &T) -> Ordering;
fn reversed(self) -> Reversed<Self>
where
Self: Sized,
{ ... }
fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U>
where
Self: Sized,
U: Comparator<T>,
{ ... }
fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F>
where
Self: Sized,
F: Fn(&T) -> U,
U: Ord,
{ ... }
}
Expand description
An interface for dealing with comparators.
Required methods
Provided methods
fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
Self: Sized,
U: Comparator<T>,
fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
Self: Sized,
U: Comparator<T>,
Chains two comparators.
Returns the result of the first comparator when it’s not Equal
.
Otherwise returns the result of the second comparator.
Examples
use comparator::{as_fn, comparing, natural_order, Comparator};
let mut v = [4, 2, 6, 3, 1, 5, 8, 7];
// Groups numbers by even-ness and sorts them.
v.sort_by(as_fn(comparing(|i| i % 2).then_comparing(natural_order())));
assert_eq!(v, [2, 4, 6, 8, 1, 3, 5, 7]);
fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
Self: Sized,
F: Fn(&T) -> U,
U: Ord,
fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
Self: Sized,
F: Fn(&T) -> U,
U: Ord,
Chains a comparator with a key function.
Returns the result of the first comparator when it’s not Equal
.
Otherwise calls f
and returns the result.