sortn is a fast, command-line utility written in Rust that performs natural sorting on input lines. Unlike standard lexicographical sorting—which would sort "file10.txt" before "file2.txt"—sortn treats multi-digit numbers as single numeric values, resulting in a more human-friendly order (1, 2, 10).
- Natural ordering: numeric parts of strings are compared as numbers (e.g.,
2<10). - Case sensitivity: sorting and uniqueness are case-sensitive by default (uppercase sorts before lowercase). Use
-i/--ignore-caseto compare case-insensitively. - Reverse:
-r/--reversereverses the sort order. - Randomize:
-n/--randomizeshuffles the input instead of sorting. - Unique:
-u/--make-uniqueremoves duplicate lines, keeping the first occurrence. Uniqueness is case-sensitive by default; use-ito make uniqueness comparisons case-insensitive. - Skip blank lines:
-b/--skip-blank-linesremoves empty or whitespace-only lines before processing; such lines will not be returned. - Whitespace handling: the underlying
natord::compareused for comparisons ignores whitespace characters (spaces and tabs). If your ordering depends on exact whitespace, preprocess input first.
- If
--randomizeis used, input is shuffled. Otherwise input is sorted. - After sorting or shuffling,
--make-unique(if present) is applied; the first occurrence of each unique key is kept. - Flags that affect comparisons:
--ignore-caseaffects both sorting and uniqueness;natord::compareitself ignores whitespace.
Download a prebuilt executable for your platform from the Releases page, or build from source (see below).
Visit the Releases page for the latest prebuilt assets:
Windows (64-bit) download link: https://github.com/dorianmeric/sortn/releases/download/v1.0.0/sortn-x86_64-pc-windows-msvc.zip
After downloading, extract and move sortn.exe to C:\Program Files\sortn\ and add that folder to your %PATH%, or move it to an existing folder already on PATH (requires admin to write to C:\Program Files).
Expand-Archive sortn-*-windows-*.zip -DestinationPath 'C:\Program Files\sortn\'
# Add to PATH permanently (requires a new shell to take effect)
setx PATH "%PATH%;C:\Program Files\sortn\"Linux (x86 64-bit) download link: https://github.com/dorianmeric/sortn/releases/download/v1.0.0/sortn-x86_64-unknown-linux-gnu.tar.xz
After downloading, extract and move to /usr/local/bin (requires sudo):
tar xzf sortn-*-linux-*.tar.gz
sudo mv sortn /usr/local/bin/
sudo chmod +x /usr/local/bin/sortnmacOS (x86 64-bit) download link: https://github.com/dorianmeric/sortn/releases/download/v1.0.0/sortn-x86_64-apple-darwin.tar.xz
After downloading, extract and move to /usr/local/bin or ~/bin:
tar xzf sortn-*-apple-*.tar.gz
sudo mv sortn /usr/local/bin/
sudo chmod +x /usr/local/bin/sortn
# or for a user-local install:
mv sortn ~/bin/
chmod +x ~/bin/sortnIf you prefer not to install globally, you can keep the binary in any folder and run it by specifying its path (for example ./sortn).
Ensure you have the Rust toolchain installed.
- Clone this repository.
- Build the release binary:
git clone https://github.com/dorianmeric/sortn.git
cd sortn
cargo build --release
- Move the binary to your path:
cp target/release/sortn /usr/local/bin/
sortn reads lines from standard input and writes results to standard output. Below are common examples and flag behaviors.
$ cat files.txt
file10.txt
file2.txt
file1.txt
$ cat files.txt | sortn
file1.txt
file2.txt
file10.txt$ cat files.txt | sortn -r
file10.txt
file2.txt
file1.txtShuffle the input instead of sorting:
$ cat files.txt | sortn -n
file2.txt
file1.txt
file10.txtKeep first occurrence of duplicate lines.
Input input.txt:
a
A
a
Case-sensitive unique (default):
$ cat input.txt | sortn -u
a
ACase-insensitive unique (use -i):
$ cat input.txt | sortn -u -i
a$ printf "a\n\n \nb\n" | sortn -b
a
bnatord::compare ignores whitespace characters when comparing lines. Example:
" a1"
"a1"
sortn will treat these as equivalent for ordering because whitespace is ignored by the comparison function; if you need to preserve whitespace differences, trim or escape them before sorting.
| Flag | Long Flag | Description |
|---|---|---|
-n |
--randomize |
Shuffle input instead of sorting. |
-r |
--reverse |
Reverse sort order. |
-i |
--ignore-case |
Case-insensitive comparisons for both sorting and uniqueness. |
-u |
--make-unique |
Remove duplicate lines (first occurrence kept). Case-sensitive by default; adding -i (-ui for short) makes matching case-insensitive. |
-b |
--skip-blank-lines |
Drop blank lines (empty or whitespace-only) before processing; they will not be returned. |
-h |
--help |
Show help information. |
-V |
--version |
Show version information. |
Note:
- when both
--randomizeand sorting flags are present,--randomizetakes precedence (input is shuffled). - After shuffling or sorting,
--make-uniqueis applied.
The standard GNU sort includes a -V (--version-sort) mode that performs natural-like ordering. sortn differs by being a focused Rust utility with these distinctions:
- Simplicity: small focused tool with a few flags.
- Performance: optimized in Rust and using
natordfor comparisons. - Memory model:
sortnloads all input into memory for sorting; for extremely large datasets, GNUsort(which can use disk) may be more appropriate.
sortn is optimized for speed by:
- Locking I/O: Accessing
stdinandstdoutthrough locked buffers to minimize system call overhead. - Efficient Comparison: Using the
natordcrate which is optimized for natural string comparison without unnecessary allocations. - Zero-Cost Abstractions: Leveraging Rust's ownership model to manage string data efficiently during the sort process.
- Documentation for the comparison algorithm: https://docs.rs/natord
- Quick example:
cat files.txt | sortn -i -r -u