diff options
| author | Havvy (Ryan Scheel) <ryan.havvy@gmail.com> | 2018-09-03 23:11:15 -0700 |
|---|---|---|
| committer | Havvy (Ryan Scheel) <ryan.havvy@gmail.com> | 2018-10-05 17:41:42 -0700 |
| commit | e36bbc82f2fc49945b4ef42ddeca8c1443c3bac4 (patch) | |
| tree | d1abd8f27b16a39a74e85ee51c86d9fd246cd367 /src/liballoc/slice.rs | |
| parent | 7e57f0a6a8fd4e5df7890613918f4a2c3b5a1fb7 (diff) | |
| download | rust-e36bbc82f2fc49945b4ef42ddeca8c1443c3bac4.tar.gz rust-e36bbc82f2fc49945b4ef42ddeca8c1443c3bac4.zip | |
Example of total ord of elements for sort_by
Diffstat (limited to 'src/liballoc/slice.rs')
| -rw-r--r-- | src/liballoc/slice.rs | 6 |
1 files changed, 6 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] |
