Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,9 @@ impl<'a> Line<'a> {
);
}
if settings.mode == SortMode::Numeric {
// exclude inf, nan, scientific notation
let line_num_float = (!line.iter().any(u8::is_ascii_alphabetic))
// exclude inf, nan, scientific notation, and a leading '+' which
// GNU sort -n doesn't accept (only -g does).
let line_num_float = (!line.iter().any(|&b| b.is_ascii_alphabetic() || b == b'+'))
.then(|| std::str::from_utf8(line).ok())
.flatten()
.and_then(|s| s.parse::<f64>().ok());
Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ fn test_numeric_unsorted_ints() {
);
}

#[test]
fn test_numeric_leading_plus_not_numeric() {
// GNU sort -n doesn't accept a leading '+' as a sign, so "+1", "+10", "+2"
// should compare lexicographically (i.e., stay in their original order
// since sort is stable on equal keys), not numerically.
new_ucmd!()
.arg("-n")
.pipe_in("+1\n+10\n+2\n")
.succeeds()
.stdout_is("+1\n+10\n+2\n");
}

#[test]
fn test_human_block_sizes() {
test_helper(
Expand Down
Loading