Example of total ord of elements for sort_by

This commit is contained in:
Havvy (Ryan Scheel) 2018-09-03 23:11:15 -07:00
parent 7e57f0a6a8
commit e36bbc82f2
2 changed files with 12 additions and 0 deletions

View file

@ -242,6 +242,12 @@ impl<T> [T] {
/// // reverse sorting
/// v.sort_by(|a, b| b.cmp(a));
/// assert!(v == [5, 4, 3, 2, 1]);
///
/// // While f64 doesn't implement Ord because NaN != NaN, we can use
/// // partial_cmp here because we know none of the elements are NaN.
/// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
/// floats.sort_by(|a, b| a.partial_cmp(b).unwrap());
/// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]

View file

@ -1367,6 +1367,12 @@ impl<T> [T] {
/// // reverse sorting
/// v.sort_unstable_by(|a, b| b.cmp(a));
/// assert!(v == [5, 4, 3, 2, 1]);
///
/// // While f64 doesn't implement Ord because NaN != NaN, we can use
/// // partial_cmp here because we know none of the elements are NaN.
/// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
/// floats.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
/// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
/// ```
///
/// [pdqsort]: https://github.com/orlp/pdqsort