Skip to content

Commit 3c80f3a

Browse files
committed
fix(sort): reject leading '+' in numeric (-n) sort
GNU sort -n does not treat '+' as a number sign. Skip f64 parsing for lines with a leading plus so they sort lexicographically like GNU. Fixes #10315
1 parent be00b37 commit 3c80f3a

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/uu/sort/src/sort.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,11 @@ impl<'a> Line<'a> {
668668
);
669669
}
670670
if settings.mode == SortMode::Numeric {
671-
// exclude inf, nan, scientific notation
671+
// exclude inf, nan, scientific notation; GNU -n does not treat '+' as a sign
672672
let line_num_float = (!line.iter().any(u8::is_ascii_alphabetic))
673673
.then(|| std::str::from_utf8(line).ok())
674674
.flatten()
675+
.filter(|s| !s.trim_ascii_start().starts_with('+'))
675676
.and_then(|s| s.parse::<f64>().ok());
676677
line_data.line_num_floats.push(line_num_float);
677678
}

tests/by-util/test_sort.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ fn test_numeric_with_trailing_invalid_chars() {
296296
);
297297
}
298298

299+
#[test]
300+
fn test_numeric_sort_rejects_leading_plus_sign() {
301+
// GNU sort -n does not treat '+' as a number sign; lines sort lexicographically.
302+
new_ucmd!()
303+
.arg("-n")
304+
.pipe_in("+1\n+10\n+2\n")
305+
.succeeds()
306+
.stdout_is("+1\n+10\n+2\n");
307+
}
308+
299309
#[test]
300310
fn test_check_zero_terminated_failure() {
301311
new_ucmd!()

0 commit comments

Comments
 (0)