mirror of
https://github.com/uutils/coreutils
synced 2024-11-16 09:48:03 +00:00
sort: treat "NaN" as string in numeric sort
This commit is contained in:
parent
07b01a85f9
commit
47f5f12759
4 changed files with 13 additions and 0 deletions
|
@ -382,8 +382,10 @@ fn permissive_f64_parse(a: &str) -> f64 {
|
|||
// On the flip side, this will give NEG_INFINITY for "1,234", which might be OK
|
||||
// because there's no way to handle both CSV and thousands separators without a new flag.
|
||||
// GNU sort treats "1,234" as "1" in numeric, so maybe it's fine.
|
||||
// GNU sort treats "NaN" as non-number in numeric, so it needs special care.
|
||||
let sa: &str = a.split_whitespace().next().unwrap();
|
||||
match sa.parse::<f64>() {
|
||||
Ok(a) if a.is_nan() => std::f64::NEG_INFINITY,
|
||||
Ok(a) => a,
|
||||
Err(_) => std::f64::NEG_INFINITY,
|
||||
}
|
||||
|
|
3
tests/fixtures/sort/numeric_floats_with_nan.expected
vendored
Normal file
3
tests/fixtures/sort/numeric_floats_with_nan.expected
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
NaN
|
||||
.02
|
||||
.03
|
3
tests/fixtures/sort/numeric_floats_with_nan.txt
vendored
Normal file
3
tests/fixtures/sort/numeric_floats_with_nan.txt
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
.03
|
||||
.02
|
||||
NaN
|
|
@ -12,6 +12,11 @@ fn test_numeric_floats() {
|
|||
test_helper("numeric_floats", "-n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_numeric_floats_with_nan() {
|
||||
test_helper("numeric_floats_with_nan", "-n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_numeric_unfixed_floats() {
|
||||
test_helper("numeric_unfixed_floats", "-n");
|
||||
|
|
Loading…
Reference in a new issue