Skip to content

Commit 7e57f0a

Browse files
committed
Doc total order requirement of sort(_unstable)_by
I took the definition of what a total order is from the Ord trait docs. I specifically put "elements of the slice" because if you have a slice of f64s, but know none are NaN, then sorting by partial ord is total in this case. I'm not sure if I should give such an example in the docs or not.
1 parent b8bea5a commit 7e57f0a

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/liballoc/slice.rs

+7
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ impl<T> [T] {
211211
///
212212
/// This sort is stable (i.e. does not reorder equal elements) and `O(n log n)` worst-case.
213213
///
214+
/// The comparator function must define a total ordering for the elements in the slice. If
215+
/// the ordering is not total, the order of the elements is unspecified. An order is a
216+
/// total order if it is (for all a, b and c):
217+
///
218+
/// * total and antisymmetric: exactly one of a < b, a == b or a > b is true; and
219+
/// * transitive, a < b and b < c implies a < c. The same must hold for both == and >.
220+
///
214221
/// When applicable, unstable sorting is preferred because it is generally faster than stable
215222
/// sorting and it doesn't allocate auxiliary memory.
216223
/// See [`sort_unstable_by`](#method.sort_unstable_by).

src/libcore/slice/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,13 @@ impl<T> [T] {
13391339
/// This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
13401340
/// and `O(n log n)` worst-case.
13411341
///
1342+
/// The comparator function must define a total ordering for the elements in the slice. If
1343+
/// the ordering is not total, the order of the elements is unspecified. An order is a
1344+
/// total order if it is (for all a, b and c):
1345+
///
1346+
/// * total and antisymmetric: exactly one of a < b, a == b or a > b is true; and
1347+
/// * transitive, a < b and b < c implies a < c. The same must hold for both == and >.
1348+
///
13421349
/// # Current implementation
13431350
///
13441351
/// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,

0 commit comments

Comments
 (0)