Function permutation::permutation::sort
source · [−]pub fn sort<T, S>(slice: S) -> Permutation where
T: Ord,
S: AsRef<[T]>,
Expand description
Return the permutation that would sort a given slice.
This calculates the permutation that if it were applied to the slice, would put the elements in sorted order.
Examples
let mut vec = vec!['z','w','h','a','s','j'];
let permutation = permutation::sort(&vec);
let permuted = permutation.apply_slice(&vec);
vec.sort();
assert_eq!(vec, permuted);
You can also use it to sort multiple arrays based on the ordering of one.
let names = vec!["Bob", "Steve", "Jane"];
let salary = vec![10, 5, 15];
let permutation = permutation::sort(&salary);
let ordered_names = permutation.apply_slice(&names);
let ordered_salaries = permutation.apply_slice(&salary);
assert_eq!(ordered_names, vec!["Steve", "Bob", "Jane"]);
assert_eq!(ordered_salaries, vec![5, 10, 15]);