Tools / Sort lines
Sort Lines Alphabetically
Sort lines A to Z, Z to A, by length, or shuffle them. Numbers sort naturally, so item2 comes before item10.
How it works
Sorting text looks trivial until numbers are involved. A naive alphabetical sort puts item10 before item2, because the character 1 comes before 2. That is wrong for anything that reads as a sequence: file names, version numbers, chapter headings, invoice lines. This tool compares with numeric awareness, so embedded numbers are ordered by value.
Comparison also ignores case and accents for ordering purposes, which keeps Apple and apple next to each other instead of splitting the list into two blocks. Sorting by length is useful for finding the outliers in a list of product names or keywords. Shuffle is deterministic for the same input, so reloading the page does not silently reshuffle what you were working on.
Edge cases
- Natural ordering means
item2sorts beforeitem10. A plain character sort would do the opposite. - Case-insensitive comparison keeps
Appleandappleadjacent rather than in separate blocks. - Shuffle is reproducible for the same input, so the result does not change when you edit an unrelated option.
Do it in code instead
The same result with a shell one-liner or a few lines of Python, if you would rather automate it:
Bash (sort)
sort list.txt # plain
sort -V list.txt # natural version order
sort -f list.txt # ignore case
Python (natural)
import re
def key(s): return [int(t) if t.isdigit() else t.lower() for t in re.split(r'(\d+)', s)]
print('\n'.join(sorted(open('list.txt').read().splitlines(), key=key)))
JavaScript
lines.sort((a,b) => a.localeCompare(b, undefined, {numeric:true, sensitivity:'base'}))
Excel
Data > Sort A to Z, then choose Options > Natural sort if available
Questions
- Why is item2 before item10?
- Because the sort is numeric-aware. It reads the digits as a number and compares 2 with 10, rather than comparing the characters one by one. For pure alphabetical behaviour on codes, zero-pad the numbers so every value has the same width.
- Is the shuffle random every time?
- No. It is seeded from the input, so the same list shuffled twice gives the same order. Change one character in the input to get a different arrangement. This is deliberate: a list that reshuffles on every keystroke is unusable.
- Does it remove blank lines?
- Only if you tick the box. By default blank lines sort to the top, because an empty string compares before any text. Ticking “drop blank lines” removes them first.