about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/liballoc/slice.rs6
-rw-r--r--src/libcore/slice/mod.rs6
2 files changed, 12 insertions, 0 deletions
diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs
index 2ded376b395..ad47eb4b70b 100644
--- a/src/liballoc/slice.rs
+++ b/src/liballoc/slice.rs
@@ -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]
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs
index c22ea0a01f8..a6e0389e66f 100644
--- a/src/libcore/slice/mod.rs
+++ b/src/libcore/slice/mod.rs
@@ -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