utilobox

How to sort lines alphabetically and get the order you expect

You have a list to put in order and the result looks wrong: item10 sits before item2, a line with a leading space jumps to the top, or duplicates vanished. This guide explains how to sort lines alphabetically with the browser tool and the ordering rules behind each option.

Last updated:

Sort lines alphabetically: the quick route

To sort lines alphabetically, open Remove duplicate lines, paste your list into ‘Input (one item per line)’, and in the ‘Order’ group open the ‘Sort’ menu and choose ‘A to Z’. The sorted list appears in ‘Result’ at once; press ‘Copy’ to take it. ‘Z to A’ is the same ordering reversed, and ‘Keep original order’ is the starting choice.

One thing catches people the first time: the tool is also a duplicate remover, and the setting ‘Keep the first of each’ is on from the start. So when you sort, repeated lines are dropped unless you change ‘Repeated lines’ to ‘Leave as they are’. Look at ‘Lines before’ and ‘Lines after’ under the boxes; if the second number is smaller than you expected, that is the reason.

How A to Z really orders text

The order is not a raw comparison of letter codes. It follows the dictionary rules of the page language, which is why it feels more natural than the order that some programs give. Letters are compared first by their base letter, so “apple”, “Apple” and “Banana” come out as apple, Apple, Banana: capitals do not push a word to the end. When two lines differ only in case, the lowercase one comes first.

Accents are treated as small differences, not as new letters. Lines such as eclair, éclair and emile sit together, in that region of the alphabet, instead of being thrown to the end. This holds for the English edition of the page; the ordering of letters such as ä or å follows the language of the page you are on, so a German or Swedish page may place them differently.

Words are compared from the first character, with no special treatment for “the” or “a”. “The Hobbit” is sorted under T, and “A Tale” under A. If you want a library-style order, remove the leading article from the list first.

Spaces, symbols and digits come before letters

Characters that are not letters have their own place. In a sorted list, lines that start with a space or a symbol come first, then lines that start with a digit, then lines that start with a letter. A line with a leading space is the typical surprise: “ banana” goes to the top of the list because the space sorts before everything else.

The cure is the ‘Trim spaces around each line’ option, which removes spaces at both ends before sorting. It is worth ticking whenever your text came from a copy, an export or a pasted table. Empty lines also go to the top, because nothing sorts before nothing; tick ‘Remove empty lines’ to discard them.

Quotation marks, brackets and bullets at the start of a line count as symbols too. A list in which some entries begin with a quote mark will split into two groups. Strip those characters before sorting, or sort the plain text and add decoration afterwards with the prefix box.

Numbers inside text: item10 and item2

In plain A to Z, digits are compared one character at a time. The line “item10” starts with item1, so it falls between item1 and item2. A list of item1, item2, item10 and item20 therefore comes out as item1, item10, item2, item20. That is correct alphabetical order and exactly what most people do not want for numbered names.

Choose ‘Natural (item2 before item10)’ instead. Natural order reads a run of digits as a single number, so the same list becomes item1, item2, item10, item20. File names, chapter titles, version labels such as 1.2 and 1.10, and rooms like A3 and A12 all need this. Natural order also ignores case when comparing, and lines that tie keep their original order.

Version numbers deserve one warning. “file 1.5” and “file 1.10” are ordered with the second as larger, which is right for software versions but wrong if you meant decimals. Decide which kind of number you have before trusting the result.

Sorting by number, by length, or reversing

‘By number, low to high’ and ‘By number, high to low’ ignore the words and look at the first number in each line. A thousands separator is understood, so 1,200 counts as 1200, and a leading minus sign is a negative number. Lines that contain no number are placed last in both directions. This is the right choice for a list such as prices or scores, where the number matters and the words do not. It is the wrong choice for dates written as day, month and year, because only the first number decides.

‘By length, short to long’ and ‘By length, long to short’ count characters, with each letter, accented or not, counted as one. Lines of the same length keep their original order. It is handy for spotting the longest entry in a list or for lining up a column by size.

‘Reverse order’ is not the same as ‘Z to A’. It flips the list that exists at that point, whatever the order was, so use it to turn the bottom of a log file to the top. ‘Random shuffle’ mixes the lines, and ‘Shuffle again’ draws a new order, which suits drawing an order for speakers or tasks.

Use the right option for the job

A name list is a good test of how you sort lines alphabetically. For names written as “Anna Lee”, A to Z sorts by first name. To sort by surname, the text itself must put the surname first, as in “Lee, Anna”; the tool cannot reorder words inside a line. Dates written year first, such as 2025-03-09, sort correctly in A to Z because the largest unit comes first, while dates written month first do not.

Sorted output can be numbered and decorated in the same pass. Tick ‘Add line numbers’ to get 1., 2., 3. in front of each line, and use ‘Text before each line’ and ‘Text after each line’ to wrap every item. These steps run after the sort, so the numbers always follow the final order.

A last step is to use ‘Use result as input’ to chain passes, for example a first pass that trims and removes duplicates, then a second that sorts naturally. All of it happens in your browser.

  • Words only: A to Z, with ‘Trim spaces around each line’ ticked.
  • Numbered names such as chapter2 and chapter10: Natural.
  • Prices or scores: By number.
  • Longest or shortest entry first: By length.
  • Newest entries first in a log: Reverse order.

Check the sorted list

After you sort lines alphabetically, read the first and last five lines. If something odd sits at the top, it is almost always a leading space, a symbol, an empty line or a quote mark. If a line is missing, look at ‘Repeated lines’. If two lines you expected to be together are apart, check whether they differ in an invisible character, which the Text compare (diff) tool can reveal.

Keep the original list somewhere until you are happy, because the result is a new text and the input is not changed, but the saved copy in this browser is replaced when you paste something else.

Questions and answers

How do I sort lines alphabetically?

Paste the list, open the ‘Sort’ menu and choose ‘A to Z’. For the opposite direction choose ‘Z to A’.

Why does item10 come before item2?

A to Z compares digits one at a time, so 1 comes before 2. Choose ‘Natural (item2 before item10)’ to compare whole numbers.

Why did duplicate lines disappear when I sorted?

‘Keep the first of each’ is selected from the start. Set ‘Repeated lines’ to ‘Leave as they are’ to keep every line.

Does sorting care about capital letters?

Mostly not: apple, Apple and Banana stay together in dictionary order. When two lines differ only by case, the lowercase one comes first.

Why is one line at the top that does not belong there?

It probably starts with a space, a symbol or nothing at all, which sort before letters. Tick ‘Trim spaces around each line’ or ‘Remove empty lines’.

Can it sort by surname?

Only if the surname comes first in each line, as in “Lee, Anna”. The tool sorts whole lines and does not rearrange words.