{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreihumabae6oknwyfsfhybrfnt2ffnc3n6j3463a4wggicboj65h6nm",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mjhrvm3edjp2"
},
"path": "/t/t-sort-by-index-and-t-sort-by-key-and-index/24172#post_1",
"publishedAt": "2026-04-14T13:29:17.000Z",
"site": "https://internals.rust-lang.org",
"tags": [
"permutation - Rust"
],
"textContent": "What do you think about chances of getting something like `sort_by_index` into `std`?\n\n\n pub fn sort_by_index<K, F>(&mut self, mut f: F)\n where\n F: FnMut(usize) -> K,\n K: Ord;\n\n pub fn sort_by_key_and_index<K, F>(&mut self, mut f: F)\n where\n F: FnMut(usize, &T) -> K,\n K: Ord;\n\n pub fn sort_unstable_by_index<K, F>(&mut self, mut f: F)\n where\n F: FnMut(usize) -> K,\n K: Ord;\n\n pub fn sort_unstable_by_key_and_index<K, F>(&mut self, mut f: F)\n where\n F: FnMut(usize, &T) -> K,\n K: Ord;\n\n // and `cached` too?\n\n\nOne 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.\n\nI don't see a good way for it to be an external crate, as it would basically have to reimplement the sorting form std.",
"title": "`<[T]>::sort_by_index` and `<[T]>::sort_by_key_and_index`"
}