Skip to main content

total_order

Function total_order 

Source
pub fn total_order<A: Float>(a: &A, b: &A) -> Ordering
Expand description

Total ordering for floating-point values that never panics.

f64::total_cmp/f32::total_cmp are inherent methods and therefore unavailable behind a generic A: Float bound, so this reproduces the same contract: a genuine total order in which NaN sorts after every real number (and equals itself). Use it instead of partial_cmp(..).expect("unwrap failed"), which panics the moment a NaN reaches the comparator, in sort_by/min_by/max_by/select_nth.

ยงExamples

use optirs_core::utils::total_order;

let mut values = vec![2.0, f64::NAN, 1.0];
values.sort_by(total_order);
assert_eq!(values[0], 1.0);
assert_eq!(values[1], 2.0);
assert!(values[2].is_nan(), "NaN sorts last");