pub fn sort_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.

This is the same as permutation::sort() except that it allows you to specify the comparator to use when sorting similar to std::slice.sort_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 or a > b is true, and
  • transitive, a < b and b < c implies a < c. The same must hold for both == and >.

Examples

let mut vec = vec!['z','w','h','a','s','j'];
let permutation = permutation::sort_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);