About Natural Sort Lines
Natural Sort Lines orders lines the way a person would read them: runs of digits are compared as numbers, not as characters. So `chapter2` sorts before `chapter10`, `v1.9` before `v1.10`, and `IMG_001.JPG` before `IMG_999.JPG` — the order you would expect from a file manager.
Use natural sort whenever your list mixes letters and numbers — file names, version strings, ticket IDs (`PROJ-1`, `PROJ-12`, `PROJ-123`), build artifacts, dated identifiers — and the alphabetic order produced by plain Sort Lines feels wrong. The non-numeric portions are still compared lexicographically, so `apple` and `banana` interleave normally; only the embedded numeric runs use numeric comparison. The line count is preserved exactly.
Examples
file10.txt
file2.txt
file1.txtfile1.txt
file2.txt
file10.txtNumeric runs compared as numbers. Plain Sort Lines would give file1.txt, file10.txt, file2.txt.
Frequently asked questions
How is this different from Sort Lines?
Sort Lines uses pure lexicographic comparison: `2` is greater than `10` because `2` > `1` at the first differing character. Natural Sort detects runs of digits and compares them as numbers, so `2` comes before `10` as a human would expect.
Does it work with leading zeros?
Yes. `001` and `1` are both treated as the number 1, so they sort together. The original text is preserved in the output — natural sort changes the order, not the content.
What about negative numbers and decimals?
Embedded `-` and `.` characters are treated as separators by default; natural sort handles unsigned integer runs. For full numeric tokens including signs and decimals, sort the numbers separately or post-process.
Is it stable?
Yes. Lines that compare equal under natural sort retain their original input order, so the operation is deterministic on inputs that contain repeated values.
