|
| 1 | +# sortn |
| 2 | + |
| 3 | +`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). |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +* **Natural Ordering**: Correctly handles numeric sequences within strings. |
| 8 | +* **Case Insensitivity**: Optional flag (`-i`) to ignore character casing during comparison. |
| 9 | +* **Reverse Sort**: Quickly invert the sorting order with the `-r` flag. |
| 10 | +* **Modern CLI**: Built with **Clap v4**, providing a clean interface and automatic help generation. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Ensure you have the Rust toolchain installed. |
| 15 | + |
| 16 | +1. Clone this repository. |
| 17 | +2. Build the release binary: |
| 18 | +```bash |
| 19 | +cargo build --release |
| 20 | + |
| 21 | +``` |
| 22 | + |
| 23 | + |
| 24 | +3. Move the binary to your path: |
| 25 | +```bash |
| 26 | +cp target/release/sortn /usr/local/bin/ |
| 27 | + |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +`sortn` reads from standard input (`stdin`) and writes the sorted result to standard output (`stdout`). |
| 35 | + |
| 36 | +### Basic Sorting |
| 37 | + |
| 38 | +```bash |
| 39 | +$ cat files.txt |
| 40 | +file10.txt |
| 41 | +file2.txt |
| 42 | +file1.txt |
| 43 | + |
| 44 | +$ cat files.txt | sortn |
| 45 | +file1.txt |
| 46 | +file2.txt |
| 47 | +file10.txt |
| 48 | + |
| 49 | +``` |
| 50 | + |
| 51 | +### Reverse Order (`-r`) |
| 52 | + |
| 53 | +```bash |
| 54 | +$ cat files.txt | sortn -r |
| 55 | +file10.txt |
| 56 | +file2.txt |
| 57 | +file1.txt |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +### Case-Insensitive (`-i`) |
| 62 | + |
| 63 | +```bash |
| 64 | +$ echo -e "B\na\nC" | sortn -i |
| 65 | +a |
| 66 | +B |
| 67 | +C |
| 68 | + |
| 69 | +``` |
| 70 | + |
| 71 | +## Options |
| 72 | + |
| 73 | +| Flag | Long Flag | Description | |
| 74 | +| --- | --- | --- | |
| 75 | +| `-r` | `--reverse` | Sort in reverse order. | |
| 76 | +| `-i` | `--ignore-case` | Perform case-insensitive natural sorting. | |
| 77 | +| `-h` | `--help` | Print help information. | |
| 78 | +| `-V` | `--version` | Print version information. | |
| 79 | + |
| 80 | +## Comparison with GNU `sort` |
| 81 | + |
| 82 | +While the standard GNU `sort` utility includes a `-V` (`--version-sort`) flag that achieves similar results, `sortn` offers several distinctions: |
| 83 | + |
| 84 | +* **Simplicity**: `sortn` is a focused, lightweight tool specifically designed for natural sorting without the overhead of the dozens of flags found in GNU `sort`. |
| 85 | +* **Performance**: Written in Rust and utilizing the `natord` crate, `sortn` provides highly competitive performance for in-memory sorting tasks. |
| 86 | +* **Memory Usage**: Currently, `sortn` loads all input lines into memory to perform a global sort. For extremely large datasets that exceed available RAM, GNU `sort` (which uses disk-based merging) may be more suitable. |
| 87 | + |
| 88 | +## Performance Benchmarks |
| 89 | + |
| 90 | +`sortn` is optimized for speed by: |
| 91 | + |
| 92 | +1. **Locking I/O**: Accessing `stdin` and `stdout` through locked buffers to minimize system call overhead. |
| 93 | +2. **Efficient Comparison**: Using the `natord` crate which is optimized for natural string comparison without unnecessary allocations. |
| 94 | +3. **Zero-Cost Abstractions**: Leveraging Rust's ownership model to manage string data efficiently during the sort process. |
| 95 | + |
0 commit comments