Skip to content

Commit d4b7915

Browse files
authored
Rollup merge of #140526 - Natr1x:sort-direction-documentation, r=dtolnay
docs: Specify that common sort functions sort in an ascending direction From [forum discussion](https://users.rust-lang.org/t/is-there-a-way-to-sort-a-slice-in-specifically-ascending-or-descending-order/128998?u=natr1x) it seems like the sorting direction can be expected to always be ascending (in terms of `cmp::Ordering`). If this is the case then it would be nice to have this stated in the documentation.
2 parents 462cc09 + b735dce commit d4b7915

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

library/alloc/src/slice.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use crate::boxed::Box;
6969
use crate::vec::Vec;
7070

7171
impl<T> [T] {
72-
/// Sorts the slice, preserving initial order of equal elements.
72+
/// Sorts the slice in ascending order, preserving initial order of equal elements.
7373
///
7474
/// This sort is stable (i.e., does not reorder equal elements) and *O*(*n* \* log(*n*))
7575
/// worst-case.
@@ -137,7 +137,8 @@ impl<T> [T] {
137137
stable_sort(self, T::lt);
138138
}
139139

140-
/// Sorts the slice with a comparison function, preserving initial order of equal elements.
140+
/// Sorts the slice in ascending order with a comparison function, preserving initial order of
141+
/// equal elements.
141142
///
142143
/// This sort is stable (i.e., does not reorder equal elements) and *O*(*n* \* log(*n*))
143144
/// worst-case.
@@ -197,7 +198,8 @@ impl<T> [T] {
197198
stable_sort(self, |a, b| compare(a, b) == Less);
198199
}
199200

200-
/// Sorts the slice with a key extraction function, preserving initial order of equal elements.
201+
/// Sorts the slice in ascending order with a key extraction function, preserving initial order
202+
/// of equal elements.
201203
///
202204
/// This sort is stable (i.e., does not reorder equal elements) and *O*(*m* \* *n* \* log(*n*))
203205
/// worst-case, where the key function is *O*(*m*).
@@ -252,7 +254,8 @@ impl<T> [T] {
252254
stable_sort(self, |a, b| f(a).lt(&f(b)));
253255
}
254256

255-
/// Sorts the slice with a key extraction function, preserving initial order of equal elements.
257+
/// Sorts the slice in ascending order with a key extraction function, preserving initial order
258+
/// of equal elements.
256259
///
257260
/// This sort is stable (i.e., does not reorder equal elements) and *O*(*m* \* *n* + *n* \*
258261
/// log(*n*)) worst-case, where the key function is *O*(*m*).

library/core/src/slice/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,7 +2986,7 @@ impl<T> [T] {
29862986
self.binary_search_by(|k| f(k).cmp(b))
29872987
}
29882988

2989-
/// Sorts the slice **without** preserving the initial order of equal elements.
2989+
/// Sorts the slice in ascending order **without** preserving the initial order of equal elements.
29902990
///
29912991
/// This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not
29922992
/// allocate), and *O*(*n* \* log(*n*)) worst-case.
@@ -3047,8 +3047,8 @@ impl<T> [T] {
30473047
sort::unstable::sort(self, &mut T::lt);
30483048
}
30493049

3050-
/// Sorts the slice with a comparison function, **without** preserving the initial order of
3051-
/// equal elements.
3050+
/// Sorts the slice in ascending order with a comparison function, **without** preserving the
3051+
/// initial order of equal elements.
30523052
///
30533053
/// This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not
30543054
/// allocate), and *O*(*n* \* log(*n*)) worst-case.
@@ -3102,8 +3102,8 @@ impl<T> [T] {
31023102
sort::unstable::sort(self, &mut |a, b| compare(a, b) == Ordering::Less);
31033103
}
31043104

3105-
/// Sorts the slice with a key extraction function, **without** preserving the initial order of
3106-
/// equal elements.
3105+
/// Sorts the slice in ascending order with a key extraction function, **without** preserving
3106+
/// the initial order of equal elements.
31073107
///
31083108
/// This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not
31093109
/// allocate), and *O*(*n* \* log(*n*)) worst-case.

0 commit comments

Comments
 (0)