[][src]Trait comparator::Comparator

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
, { ... } }

An interface for dealing with comparators.

Required methods

fn compare(&self, a: &T, b: &T) -> Ordering

Compares its two arguments for order.

Loading content...

Provided methods

fn reversed(self) -> Reversed<Self> where
    Self: Sized

Reverses the Comparator.

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

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.

Loading content...

Implementors

impl<F, T> Comparator<T> for F where
    F: Fn(&T, &T) -> Ordering,
    T: ?Sized
[src]

fn reversed(self) -> Reversed<Self> where
    Self: Sized
[src]

fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
    Self: Sized,
    U: Comparator<T>, 
[src]

fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
    Self: Sized,
    F: Fn(&T) -> U,
    U: Ord
[src]

impl<F, T, U> Comparator<T> for Comparing<F> where
    F: Fn(&T) -> U,
    T: ?Sized,
    U: Ord
[src]

fn reversed(self) -> Reversed<Self> where
    Self: Sized
[src]

fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
    Self: Sized,
    U: Comparator<T>, 
[src]

fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
    Self: Sized,
    F: Fn(&T) -> U,
    U: Ord
[src]

impl<T, F, U, W> Comparator<U> for ThenComparingByKey<T, F> where
    T: Comparator<U>,
    F: Fn(&U) -> W,
    U: ?Sized,
    W: Ord
[src]

fn reversed(self) -> Reversed<Self> where
    Self: Sized
[src]

fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
    Self: Sized,
    U: Comparator<T>, 
[src]

fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
    Self: Sized,
    F: Fn(&T) -> U,
    U: Ord
[src]

impl<T, U> Comparator<U> for Reversed<T> where
    T: Comparator<U>,
    U: ?Sized
[src]

fn reversed(self) -> Reversed<Self> where
    Self: Sized
[src]

fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
    Self: Sized,
    U: Comparator<T>, 
[src]

fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
    Self: Sized,
    F: Fn(&T) -> U,
    U: Ord
[src]

impl<T, U, W> Comparator<W> for ThenComparing<T, U> where
    T: Comparator<W>,
    U: Comparator<W>,
    W: ?Sized
[src]

fn reversed(self) -> Reversed<Self> where
    Self: Sized
[src]

fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
    Self: Sized,
    U: Comparator<T>, 
[src]

fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
    Self: Sized,
    F: Fn(&T) -> U,
    U: Ord
[src]

impl<T: ?Sized> Comparator<T> for NaturalOrder where
    T: Ord
[src]

fn reversed(self) -> Reversed<Self> where
    Self: Sized
[src]

fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
    Self: Sized,
    U: Comparator<T>, 
[src]

fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
    Self: Sized,
    F: Fn(&T) -> U,
    U: Ord
[src]

impl<T: ?Sized> Comparator<T> for ReverseOrder where
    T: Ord
[src]

fn reversed(self) -> Reversed<Self> where
    Self: Sized
[src]

fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U> where
    Self: Sized,
    U: Comparator<T>, 
[src]

fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F> where
    Self: Sized,
    F: Fn(&T) -> U,
    U: Ord
[src]

Loading content...