Skip to content

feat(sort): add -h/--human-numeric-sort - #216

Merged
ferdingler merged 1 commit into
strands-agents:mainfrom
ferdingler:feat/sort-human-numeric
Nov 12, 2026
Merged

ferdingler merged 1 commit into
strands-agents:mainfrom
ferdingler:feat/sort-human-numeric

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

sort -h printed the usage text and exited 0, so something like du | sort -h quietly turned into help text in the middle of a pipeline:

$ printf '2K\n1M\n' | sort -rh
Usage: sort [OPTIONS] [FILE]...
Sort lines of text.
...
$ echo $?
0

Now -h (and --human-numeric-sort) sorts human-readable sizes the way GNU does: first by sign and suffix (K/k, M, G, ... Y), then by the number. It also works as a key flag, like -k2h or -k2,2hr. --help still prints the help, and the other sort modes don't change.

I compared it against GNU sort on a mix of sizes, negatives, zeros, blanks and plain text, with -r, -s, -k and -t, and the output matched. One gap I left out: when two lines compare as equal, GNU breaks the tie by comparing the whole line, and -u drops lines that are equal by value. This sort doesn't do either of those today, and -n has the same problem, so I think it belongs in its own fix.

Related Issues

Part of #207

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler
ferdingler force-pushed the feat/sort-human-numeric branch from c79c4f1 to db19566 Compare November 12, 2026 19:33
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 19:34
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 19:34

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: approve. The -h comparator matches GNU sort 9.1 on everything realistic I threw at it (~1,900 differential cases, no panics). There's one 🟡 that doesn't block: sort -hu can print lines that are byte-for-byte identical.

🟡 -hu keeps exact duplicates when a value-equal line sorts between them. printf '1K\n1k\n1K\n' \| sort -hu prints 1K 1k 1K, while GNU prints 1K. The cause is src/commands/sort.rs:266-274: dedup_by compares the raw key text instead of using the sort comparator, so it only drops adjacent identical lines. This goes a bit beyond the gap you scoped out, because -u ends up emitting real duplicates. -n already has the same bug on main (1/01/1). The fix is one line, so it could go here or in your follow-up (see below and the inline test suggestion).

📝 Docs: harness-sdk site/src/content/docs/user-guide/shell/commands.mdx:35 still says No ... -h (-h incorrectly prints help). That needs a small follow-up PR in that repo once this merges.

Fix for sort.rs:266-274 (verified)

It's outside the diff, so I can't attach a suggestion block there:

    if unique {
        lines.dedup_by(|a, b| cmp(a, b) == std::cmp::Ordering::Equal);
    }

This matches GNU (-u uses the comparator) and fixes -n too (1/01/1 → 1). With this change and the stronger test from the inline suggestion, cargo test --test shell_integration sort passes 46/46. The stronger test fails at db19566 (left: "1K\n1k\n1K\n1M").

✅ What I verified
  • Head db19566: cargo test --test shell_integration sort → 46 passed, 0 failed. The full shell_integration run had 1631 passed.
  • Ran a differential against LC_ALL=C /usr/bin/sort -s (GNU 9.1) with the patterns below. All matched.
    • du -h-style output, including CRLF
    • negatives and zeros (-0K, -.5K)
    • inputs like +5K, 1,000K, 1KiB, 1e3, KK, ., -
    • flag combos: -rh, -k2h, -t: -k2,2hr, -bk2h, -hf, multiple files
  • Panic probes (1é, -é, .é, 400-digit numbers) ran cleanly. scan_number only advances over ASCII, so s[..i] can't split a UTF-8 character.
  • sort --help still prints usage. All 7 new expectations match GNU, and every one of them fails on main.
  • I didn't run clippy (it isn't installed in my sandbox). CI was still pending when I reviewed.
Appendix: non-blocking, or already on main (6)
  • ⚪ GNU rejects sort -hn (options '-hn' are incompatible, rc=2). This PR silently uses -h instead. It's a contrived combination, and leaving it lenient seems fine.
  • ⚪ --human-numeric-sort works but isn't in the --help text (sort.rs:9 lists only -h).
  • Already on main, same as -n: global ordering flags get ORed into a key that has its own flags (sort -h -k2r). GNU doesn't inherit them in that case.
  • Already on main: -k2.2h / -k2.2n fail with invalid digit found in string (parse_key_spec, sort.rs:84-85), because start.char offsets aren't supported. Separately, -kN stops at field N instead of running to end of line (extract_key).
  • Already on main: an f64 tie on numbers with 20+ digits. The -n path behaves the same.
  • Already on main: trim_start() treats a leading \r, NBSP, or other Unicode space as blank, while GNU in the C locale skips only space and tab.

Comment thread tests/shell_integration.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for the differential checks. I confirmed the shared -u issue and explained the deferral in the inline thread. The remaining existing sort gaps stay outside this change.

The command-reference entry in harness-sdk also needs updating after this merges. I'm keeping that as an explicit docs follow-up so the published command list doesn't claim support before the feature lands.

The rebased head passed the full local Rust suite, doc tests, formatting, clippy, and docs with warnings denied. Eight CLI comparisons against system sort also passed. CI is still finishing on this revision.

@ferdingler
ferdingler merged commit 83685f4 into strands-agents:main Nov 12, 2026
31 of 60 checks passed
@ferdingler
ferdingler deleted the feat/sort-human-numeric branch November 12, 2026 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants