fn value_nans_last<T: Float>(a: &T, b: &T) -> Ordering { match (a, b) { (x, y) if x.is_nan() && y.is_nan() => Ordering::Equal, (x, _) if x.is_nan() => Ordering::Greater, (_, y) if y.is_nan() => Ordering::Less, (_, _) => a.partial_cmp(b).unwrap() } } xs.sort_by(value_nans_last);