How can I co-sort two Vecs based on the values in one of the Vecs?

I just wrote a crate “permutation” that allows you to do this 🙂

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!(ordered_names == vec!["Steve", "Bob", "Jane"]);
assert!(ordered_salaries == vec![5, 10, 15]);

It likely will support this in a single function call in the future.

Leave a Comment