External Publication
Visit Post

`<[T]>::sort_by_index` and `<[T]>::sort_by_key_and_index`

Rust Internals [Unofficial] April 14, 2026
Source

To avoid internal allocation you could oblige caller to supply a scratch array of [usize; N].

EDIT: Concretely, suppose we added this function to slices

/// for 0 <= i < self.len() move item i to position permutation[i]
/// panics if self.len() != permutation.len()
/// (if possible make that a type constraint)
/// or if permutation is not actually a permutation of 0..self.len()
pub fn permute(&mut self, permutation: &[usize])

then I think the existing <[usize]>::sort_(unstable_)by_key could do the rest of the job?

Discussion in the ATmosphere

Loading comments...