`<[T]>::sort_by_index` and `<[T]>::sort_by_key_and_index`
Rust Internals [Unofficial]
April 14, 2026
What do you think about chances of getting something like sort_by_index into std?
pub fn sort_by_index<K, F>(&mut self, mut f: F)
where
F: FnMut(usize) -> K,
K: Ord;
pub fn sort_by_key_and_index<K, F>(&mut self, mut f: F)
where
F: FnMut(usize, &T) -> K,
K: Ord;
pub fn sort_unstable_by_index<K, F>(&mut self, mut f: F)
where
F: FnMut(usize) -> K,
K: Ord;
pub fn sort_unstable_by_key_and_index<K, F>(&mut self, mut f: F)
where
F: FnMut(usize, &T) -> K,
K: Ord;
// and `cached` too?
One possible use case is to sort one array by keys from some other array. It comes up very frequently for me, and for data oriented programming in general. permutation - Rust can do this, but it requires computing intermediate permutation and additional heap space.
I don't see a good way for it to be an external crate, as it would basically have to reimplement the sorting form std.
Discussion in the ATmosphere