Function permutation::permutation::sort_unstable_by
source · [−]pub fn sort_unstable_by<T, S, F>(slice: S, compare: F) -> Permutation where
S: AsRef<[T]>,
F: FnMut(&T, &T) -> Ordering,
Expand description
Return the permutation that would sort a given slice by a comparator, but might not preserve the order of equal elements.
This is the same as permutation::sort_unstable()
except that it allows you to specify
the comparator to use when sorting similar to std::slice.sort_unstable_by()
.
If the comparator does not define a total ordering, the order of the elements is unspecified.
Per the Rust Docs,
an order is a total order if it is (for all a
, b
and c
):
- total and antisymmetric: exactly one of
a < b
,a == b
ora > b
is true, and - transitive,
a < b
andb < c
impliesa < c
. The same must hold for both==
and>
.
Examples
let mut vec = vec!['z','w','h','a','s','j'];
let permutation = permutation::sort_unstable_by(&vec, |a, b| b.cmp(a));
let permuted = permutation.apply_slice(&vec);
vec.sort_by(|a,b| b.cmp(a));
assert_eq!(vec, permuted);