How to Sort Lines Alphabetically
Sorting a list of lines looks trivial until the list contains numbers. Alphabetical order puts 10 before 2, because it compares characters rather than values, and that is the single most common sorting surprise.
Text sorting
Text sorts by character code. Digits come before uppercase letters, which come before lowercase letters in most encodings, which is why a list can look unsorted when it is not.
For example, Apple, Banana, apple, banana sorted by code puts the uppercase items first. Case-insensitive sorting gives a more natural order but loses the grouping.
Paste a list to sort it A to Z, Z to A or by length.
Open the Sort Lines Alphabetically →The number problem
As text, 10 comes before 2 because the first character, 1, is lower than 2. To sort numerically you have to compare values rather than characters.
| List | Text sort | Numeric sort |
|---|---|---|
| 1, 2, 10, 20 | 1, 10, 2, 20 | 1, 2, 10, 20 |
| 9, 81, 100 | 100, 81, 9 | 9, 81, 100 |
Any list containing numbers longer than one digit needs numeric sorting, or zero-padding to equal width.
Zero padding
Padding numbers to a fixed width makes text sorting behave like numeric sorting. 001, 002, 010, 020 sorts correctly as text.
This is why file names are often numbered with leading zeros, and why ISO dates sort correctly: they are padded by design.
Dates and times
Dates written as YYYY-MM-DD sort correctly as text, because the largest unit comes first. Dates written as DD/MM/YYYY do not, because the sort groups by day.
If you have a column of dates in a local format, convert to ISO before sorting rather than trying to fix the order afterwards.
Case sensitivity
Case-sensitive sorting is predictable and reproducible. Case-insensitive sorting reads better for names and titles.
Pick one and say which. Mixing the two in different tools is why the same list can appear in two orders on two machines.
Accents and non-Latin scripts
Accented letters usually sort next to their unaccented forms in a locale-aware sort, and far away in a code-point sort. Arabic, Hebrew and other scripts have their own collation rules.
If a list mixes scripts, use a locale-aware sort or accept that the order will look arbitrary to some readers.
Mixed lists
A list with names, numbers and dates has no single correct order. Decide what the primary key is, sort on that, and put the rest in a separate column.
Trying to sort a genuinely mixed list usually produces something that looks wrong to everyone, because there is no shared rule.
Stable sorting
A stable sort preserves the original order of items that compare equal. That matters when you sort twice, for example by date and then by name: with a stable sort, the date order survives inside each name group.
Most tools use a stable sort now, but it is worth knowing the term when a two-level sort does not behave as expected.
Sorting and duplicates
Sorting brings duplicates next to each other, which makes them easy to find and remove. That is why deduplication usually starts with a sort.
Sort first, then remove adjacent duplicates, then sort again if the final order matters.
Common mistakes
- Sorting numbers as text.
- Forgetting leading zeros.
- Mixing case-sensitive and insensitive sorts.
- Sorting a date column in a local format.
- Assuming a spreadsheet and a text editor will give the same order.
A quick check
Look at the first and last three items after sorting. In a numeric list they should be the smallest and largest values; if the first item starts with a 1 and the second with a 2 while a 9 appears at the end, the sort was alphabetical.
That check takes five seconds and catches almost every sorting error.
When not to sort
Order can carry meaning. A list of steps, a ranked list or a timeline should not be alphabetised, even if it looks untidy.
If you need both, keep the original in one column and the sorted version in another rather than overwriting.
Frequently asked questions
Why does 10 come before 2 alphabetically?
Because sorting compares characters, and the character 1 is lower than 2.
How do I sort numbers correctly?
Use a numeric sort, or pad the numbers with leading zeros so text order matches value order.
Does sorting remove duplicates?
No. It brings them together so you can remove them, but it does not delete them.