We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ff2d506 commit ea6a1bdCopy full SHA for ea6a1bd
src/liballoc/slice.rs
@@ -1328,10 +1328,18 @@ impl<T> [T] {
1328
/// ```
1329
#[stable(feature = "slice_sort_by_key", since = "1.7.0")]
1330
#[inline]
1331
- pub fn sort_by_key<B, F>(&mut self, mut f: F)
+ pub fn sort_by_key<B, F>(&mut self, f: F)
1332
where F: FnMut(&T) -> B, B: Ord
1333
{
1334
- merge_sort(self, |a, b| f(a).lt(&f(b)));
+ let mut indices: Vec<_> = self.iter().map(f).enumerate().map(|(i, k)| (k, i)).collect();
1335
+ indices.sort();
1336
+ for i in 0..self.len() {
1337
+ let mut index = indices[i].1;
1338
+ while index < i {
1339
+ index = indices[index].1;
1340
+ }
1341
+ self.swap(i, index);
1342
1343
}
1344
1345
/// Sorts the slice, but may not preserve the order of equal elements.
0 commit comments