Command Palette

Search for a command to run...

How to Sort Lines of Text: Alphabetical, Natural and Numeric Order Explained

How to Sort Lines of Text: Alphabetical, Natural and Numeric Order Explained

T
Toolz Team
|Sep 19, 2026|13 min read
Prefer Toolz.dev on Google

Part of the Text Tools collection

Line Sorter

Sort lines of text alphabetically, numerically, or by length, reverse or shuffle them, and remove duplicates, blank lines, and whitespace - all in your browser.

Use the Line Sorter

Sorting a list sounds like the most solved problem in computing, until the result comes back with file10 above file2, every capitalized name bunched at the top, $5 stranded at the bottom of a price list, and "Apple" and "apple" counted as two different entries. Each of those is the sort doing exactly what it was told. The trouble is that "alphabetical order" hides several different rules, and picking the wrong one produces a list that is technically sorted and practically useless.

This guide explains the ordering rules that matter for everyday lists: byte order, locale-aware alphabetical order, natural order for embedded numbers, true numeric order and length order. It also covers how cleaning steps like trimming and removing duplicates interact with sorting. The worked examples use the Toolz line sorter, and each output shown is what its code produces.

TL;DR: To sort lines of text, paste one item per line and choose a mode. Alphabetical in the Toolz Line Sorter is locale-aware and natural, so item2 comes before item10, and Ignore case (on by default) keeps "Apple" and "apple" together. Numeric mode orders by the number at the start of each line and sinks non-numeric lines to the bottom. Length mode orders by character count. Cleaning runs before sorting: trim whitespace, drop blank lines, then remove duplicates keeping the first occurrence. The online line sorter is client-side, free, no signup, and updates as you type.

What does a line sorter do?

A line sorter takes a block of text, treats each line as one item, and rearranges the lines by a chosen rule. The same tool usually cleans the list at the same time: trimming stray spaces, removing blank lines and removing duplicates.

The Toolz version offers five modes and four cleaning options:

Setting What it does Default
A to Z Locale-aware alphabetical order with natural number handling Selected
Numeric Orders by the number each line starts with
Length Orders by character count, shortest first
Reverse Flips the current line order without sorting
Shuffle Random order; click again to reshuffle
Descending Flips the result of A to Z, Numeric or Length Off
Remove blank lines Drops lines that are empty or only whitespace On
Remove duplicates Keeps the first occurrence of each line Off
Trim whitespace Removes spaces and tabs at both ends of each line Off
Ignore case Compares lines as lowercase for sorting and duplicates On

The output panel shows how many lines came out and how many were removed, so you can see the effect of the cleaning options at a glance.

Why does file10 come before file2?

Because a plain text sort compares strings one character at a time, from the left. file10 and file2 match for four characters, then the fifth character is 1 in one and 2 in the other. The character 1 sorts before 2, so file10 wins, and the comparison never looks at the fact that 10 is a bigger number than 2.

Natural sorting fixes this by reading each run of digits as a number. The Toolz line sorter's A to Z mode does that, using the browser's built-in collation with numeric comparison turned on, the option described for String.prototype.localeCompare on MDN. The ordering rules underneath come from the Unicode Collation Algorithm, which is how browsers decide that "é" sorts near "e" rather than after "z".

Here is the same five-item list under three rules:

Input Byte order (LC_ALL=C sort) Character order, case folded Natural order (Toolz A to Z)
item10 Item1 Item1 Item1
item2 item10 item10 item2
Item1 item2 item2 item3
item3 item3 item3 item10
zebra zebra zebra zebra

Byte order puts Item1 first only because an uppercase I has a lower code than any lowercase letter. Character order with case folding fixes the capital, but still puts item10 before item2. Natural order gets both right.

How does case affect alphabetical sorting?

In raw byte order every uppercase letter sorts before every lowercase letter, so a list of names can split into two alphabetical runs: all the capitalized entries first, then all the lowercase ones. Locale-aware sorting does not do that; it treats letters as the same letter first and only uses case to break ties.

The Ignore case option goes a step further and compares everything as lowercase. When two lines differ only by case, they compare as equal, and the sort keeps them in the order they appeared in the input. That matters for duplicates, covered below.

Worked example with the built-in sample

Click "Load sample" and the input is ten lines: banana, Apple, cherry, apple, 10, 2, Banana, date, a blank line, and grape.

Settings Output Count shown
Defaults: A to Z, Ignore case on, Remove blank lines on 2, 10, Apple, apple, banana, Banana, cherry, date, grape 9 lines, 1 removed
Add Remove duplicates 2, 10, Apple, banana, cherry, date, grape 7 lines, 3 removed
Remove duplicates, Ignore case off 2, 10, apple, Apple, banana, Banana, cherry, date, grape 9 lines, 1 removed
Defaults plus Descending grape, date, cherry, Banana, banana, apple, Apple, 10, 2 9 lines, 1 removed

Three details are visible in that table. Numbers come before letters in alphabetical mode, and 2 precedes 10 because of natural ordering. With Ignore case on, Apple stays ahead of apple because it appeared first in the input. With Ignore case off, the browser's collation places the lowercase form just ahead of the uppercase one, which is the usual behavior in English-language browsers, and none of the lines count as duplicates because they are not identical. Descending is a full reversal of the ascending result, so tied lines flip too.

Alphabetical vs numeric sorting: when should you use each?

Use alphabetical (natural) mode for text that contains numbers, such as filenames, version labels or street addresses. Use numeric mode when each line is a number, or starts with one, and you want the lines ordered by value.

Numeric mode reads the number at the start of each line the way JavaScript's parseFloat does. It skips leading spaces, reads an optional sign, digits, a decimal point and an exponent, and stops at the first character that cannot continue a number. Lines that do not start with a number go to the bottom and keep their original order.

Line Value used Note
42 42
-7.5 -7.5 Negative and decimal values work
1e3 1000 Scientific notation is read
12 apples 12 Text after the number is ignored
1,000 1 Reading stops at the comma
$5 not a number Sinks to the bottom
v2 not a number Sinks to the bottom

Sorting $5, 42, 1,000, -7.5, 12 apples, 1e3 in numeric mode gives:

-7.5
1,000
12 apples
42
1e3
$5

Two traps are in that output. 1,000 sorted as 1, because thousands separators end the number. Strip them first with find and replace if your data uses them. And $5 fell to the bottom; remove currency symbols before sorting prices.

Descending in numeric mode reverses the whole result, which means the non-numeric lines move from the bottom to the top. If you only want the numbers in descending order, clean out the non-numeric lines first.

How does sorting by length work?

Length mode orders lines by how many characters they contain, shortest first, or longest first with Descending. Lines of equal length keep their input order.

The count is the JavaScript string length, which counts UTF-16 code units. For ordinary letters, digits and accented Latin characters that equals the number of visible characters: café is 4. Most emoji and some rare characters use two code units, so ok👍 counts as 4, not 3. For lists of words, names or URLs this rarely matters.

Length sorting is handy for spotting outliers: the unusually long product title, the truncated entry, or the shortest keywords in a list.

In what order does cleaning and sorting happen?

The steps always run in a fixed sequence, and the order explains most surprising results:

  1. Split the input into lines, accepting both Unix and Windows line endings.
  2. Trim each line, if Trim whitespace is on.
  3. Remove blank lines, if on. A line containing only spaces counts as blank, even with Trim off.
  4. Remove duplicates, if on. The first occurrence is kept. With Ignore case on, lines that differ only by case count as duplicates, and the casing of the first one survives.
  5. Sort, reverse or shuffle what is left.

Because trimming is optional and off by default, apple and apple (with a trailing space) are two different lines when duplicates are compared, unless you turn Trim on. That is the most common reason a "remove duplicates" pass seems to miss entries. Tabs pasted from a spreadsheet cause the same effect.

For heavier duplicate work, such as keeping the last occurrence or comparing only part of each line, the duplicate line remover guide covers the options in more depth.

Reverse vs Descending: what is the difference?

Reverse flips the current order of the lines without sorting anything. c, a, b becomes b, a, c. Descending sorts first and then flips, so c, a, b in A to Z with Descending becomes c, b, a.

Use Reverse when the original order means something, such as a log file with oldest entries first, and you want newest first. Use Descending when you want a sorted list from Z to A or largest to smallest. The Descending button is disabled in Reverse and Shuffle modes because it has nothing to flip.

Is the shuffle random enough?

The shuffle uses the Fisher-Yates algorithm, which gives every possible order an equal chance when its random numbers are uniform, and it draws those numbers from the browser's Math.random(). That is fine for randomizing a reading list, a playlist or a presentation order. It is not a cryptographic random source, so it is the wrong choice for anything where someone might have a reason to predict or dispute the result, such as a prize draw. For those, a dedicated list randomizer is the better fit. Click Shuffle again to get a new order.

Is it safe to sort sensitive lists online?

With this tool, yes. Sorting and cleaning happen client-side, in JavaScript in your browser tab. The text is not uploaded or stored, and the tool keeps working with the network off once the page has loaded. Lists of email addresses, customer names or internal hostnames never leave your machine.

The practical size limit is your browser's memory rather than a server cap. Tens of thousands of lines sort instantly on a typical laptop. Very large files, in the hundreds of megabytes, are better handled on the command line.

What are common line sorting mistakes?

Symptom Cause Fix
file10 above file2 in another tool Plain character comparison Use natural sort (A to Z here) or sort -V
Capitalized entries all at the top Byte-order sort Use a locale-aware sort or Ignore case
Duplicates not removed Trailing spaces or tabs differ Turn on Trim whitespace
"Apple" and "apple" both kept Ignore case is off Turn Ignore case on
1,000 sorted as 1 Thousands separator ends the number Remove commas before numeric sorting
Prices with $ at the bottom Line does not start with a number Strip currency symbols first
Descending numeric list starts with words Non-numeric lines are reversed to the top Remove non-numeric lines first
Output has one more "removed" than expected A trailing newline created an empty last line Expected; the blank line was dropped

Alternatives to an online line sorter

Tool How to sort Notes
GNU sort sort -f (fold case), sort -n (numeric), sort -V (natural), sort -u (unique), sort -r (reverse) Scriptable, handles huge files; see the GNU sort manual
shuf shuf list.txt Random order on the command line
Sort by length in a shell awk '{ print length, $0 }' list.txt | sort -n | cut -d' ' -f2- Counts bytes or characters depending on locale
Spreadsheet Paste into a column, Data, Sort Good when lines have multiple fields
Code editors "Sort Lines" commands in many editors Behavior varies by editor
Toolz Line Sorter Paste, click a mode Live preview, cleaning in one pass, client-side

Note that sort output depends on the LC_ALL or LANG locale setting, which is why the same command can give different orders on two machines. Setting LC_ALL=C forces byte order.

Sorting a list, step by step

  1. Open the text line sorter and paste your list, one item per line.
  2. Choose A to Z for words and filenames, Numeric for numbers, or Length for character counts.
  3. Turn on Trim whitespace if the list was pasted from a document or spreadsheet.
  4. Turn on Remove duplicates if you want each entry once; leave Ignore case on to merge different capitalizations.
  5. Check the output count and the "removed" number.
  6. Click Copy.

If the list also needs its capitalization normalized, the case converter guide explains title, sentence and other cases, and the text tools hub collects the rest of the text utilities in one place.

Comments

0 comments

0/2000 characters

No comments yet. Be the first to share your thoughts!