Lets say I have two slices
b of same size. I would like to sort
a with keys contained in
Is there an easy way to achieve this ?
What I’ve tried:
sort_by_key: it could be easy if the index of the element was passed to the extraction function, then i would just need to index
bwith that same index, unfortunately it takes a reference to the element itself.
- by zipping:
let mut zipped = a.into_iter().zip(b).collect::<Vec<_>>(); zipped.as_mut_slice().sort_by_key(|(_a, b)| *b); let (sorted_a, sorted_b): (Vec<_>, Vec<_>) = zipped.into_iter().unzip();
Unfortunately this is very cumbersome and requires to copy the two arrays.
I wish there was a function
sort_by_index_key where the extraction key would take the index of the current value as its argument to be able to achieve this.