Line Sorter Guide: Sort Lists Alphabetically, Numerically, or by Length
Sorting lines of text is one of those tasks that sounds simple but hides surprising complexity. Alphabetical order depends on whether comparison is case-sensitive. Numerical sort differs from alphabetical sort. "Natural sort" (where file10 comes after file9, not before file2) is different again. This guide covers the different sorting modes and when to use each.
Types of Sort
| Sort type | Description | Example |
|---|---|---|
| Alphabetical AāZ | Dictionary order, case-sensitive by default | apple, banana, cherry |
| Alphabetical ZāA | Reverse alphabetical | cherry, banana, apple |
| Numerical ascending | By numeric value, not lexicographic | 1, 2, 10, 20, 100 |
| Numerical descending | Highest value first | 100, 20, 10, 2, 1 |
| By length (short first) | Shorter lines first | cat, bird, banana |
| By length (long first) | Longer lines first | banana, bird, cat |
| Natural sort | Handles embedded numbers intelligently | file1, file2, file10 |
| Random / shuffle | Randomise the order | Unpredictable |
Lexicographic Sort vs Natural Sort
This is the most common sorting surprise. In lexicographic (alphabetical) order, strings are compared character by character. Since '1' < '2' < '9', but '1' also comes before '2' in '10' vs '2', the string "10" sorts before "2" lexicographically:
// Lexicographic sort (wrong for filenames)
["file1", "file10", "file2", "file20"].sort()
// Result: ["file1", "file10", "file2", "file20"]
// file10 before file2 ā NOT what humans expect
// Natural sort (correct for filenames)
["file1", "file10", "file2", "file20"].sort((a, b) =>
a.localeCompare(b, undefined, {numeric: true})
)
// Result: ["file1", "file2", "file10", "file20"]
Always use natural sort for filenames, version numbers, numbered list items, or any strings that mix text and numbers.
Case Sensitivity in Sorting
In standard ASCII ordering, all uppercase letters (AāZ, codes 65ā90) come before all lowercase letters (aāz, codes 97ā122). This means a case-sensitive sort puts Banana before apple, which is often not what people expect.
// Case-sensitive sort (default in most languages)
["banana", "Apple", "cherry"].sort()
// Result: ["Apple", "banana", "cherry"]
// Capital A comes before lowercase b
// Case-insensitive sort
["banana", "Apple", "cherry"].sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase())
)
// Result: ["Apple", "banana", "cherry"]
// Same result here, but Apple sorts with A items, not before all lowercase
Common Use Cases
Alphabetising CSS Property Lists
Many style guides recommend alphabetising CSS properties within each rule. Sorting them makes it easier to check if a property is already defined and prevents duplicates. Some tools like Stylelint can enforce this automatically, but the online sorter works well for a quick one-time cleanup.
Organising Import Statements
In Python and JavaScript, import statements are often sorted alphabetically by convention (and enforced by linters like isort or ESLint import/order). Sorting imports consistently reduces merge conflicts when multiple developers add imports to the same file.
Sorting Configuration Keys
JSON and YAML configuration files are easier to navigate when keys are sorted alphabetically. It also makes diffs cleaner ā a new key always appears in the same predictable position.
Creating Ordered Wordlists
SEO keyword lists, dictionary wordlists, and glossaries all benefit from alphabetical sorting. A sorted wordlist is easier to scan, and deduplication works more efficiently on sorted input (adjacent duplicates only need one comparison).
If you want both sorted output and no duplicates, sort first and then run duplicate removal ā or use sort -u on the command line which does both in one step. If you care about preserving original order while only removing duplicates, see the Duplicate Line Remover guide.
Sort Any List of Lines Instantly
Paste your lines and sort them alphabetically, numerically, by length, or randomly ā with case-sensitive or case-insensitive options.
Open the Line SorterSorting in Different Tools
Terminal (Linux / macOS)
sort file.txt # alphabetical A-Z
sort -r file.txt # reverse (Z-A)
sort -n file.txt # numerical sort
sort -rn file.txt # numerical descending
sort -f file.txt # case-insensitive
sort -u file.txt # sort + remove duplicates
sort -V file.txt # version/natural sort (GNU sort)
Python
lines = open('file.txt').read().splitlines()
# Alphabetical
lines.sort()
# Case-insensitive
lines.sort(key=str.lower)
# By length
lines.sort(key=len)
# Natural sort (requires natsort library)
from natsort import natsorted
lines = natsorted(lines)
How to Use the Line Sorter Tool
- Open the Line Sorter
- Paste your lines into the input area
- Select the sort mode: AāZ, ZāA, numeric, by length, or random
- Toggle case-sensitive or case-insensitive as needed
- Click Sort and copy the output