External Publication
Visit Post

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

Rust Internals [Unofficial] April 14, 2026
Source

Ddystopia:

but it requires computing intermediate permutation and additional heap space.

Note that https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by_cached_key does that internally, so maybe you could find a way to encode it in that?

Though that doesn't guarantee the order of the calls to the key generator (just that it's at most once per item), so you can't just calculate the index inside the closure.

Discussion in the ATmosphere

Loading comments...