How to Sort Lines Alphabetically (and Shuffle Them) – Online, Excel, Notepad++, Terminal
· 5 minutes read

Sorting a list sounds trivial until the list has numbers in it ("item 10" lands before "item 2"), accented names ("Ádám" ends up after "Zoe"), stray spaces or duplicates. This guide shows how to sort lines alphabetically the right way, and how to shuffle them when you need a random order.
The fastest way: sort lines online
- Open the sort lines tool.
- Paste your list, one item per line.
- Pick the order: A → Z, Z → A, Natural, By length or Reverse order.
- Optionally tick Remove duplicates, Remove empty lines or Trim spaces.
- Copy the result – or click Shuffle lines for a random order.
What makes it different from a naive sort:
- Accents sort correctly. "Ádám" goes next to "Adam", not after "Z".
- Natural order. Numbers inside text are compared as numbers, so
file 2comes beforefile 10. - Slovak alphabet. With the Slovak alphabet selected, "ch" sorts after "h" (hrad, chlieb, ihla), as Slovak and Czech dictionaries require.
- Fair shuffle. Shuffling uses the Fisher–Yates algorithm with the browser's cryptographic random number generator, so every order is equally likely – fine for raffles, quiz questions or random groups.
Input A → Z Natural
item 10 item 1 item 1
item 2 item 10 item 2
item 1 item 2 item 10
Excel
Select the column and use Data → Sort A to Z (or Sort Z to A). In Excel 365 you can also use a formula that updates automatically:
=SORT(A1:A100) ascending
=SORT(A1:A100, , -1) descending
=SORT(UNIQUE(A1:A100)) sorted, without duplicates
To shuffle in Excel 365: =SORTBY(A1:A100, RANDARRAY(ROWS(A1:A100))). In older versions, add a helper column with =RAND(), sort by it, then delete it.
Excel sorts text "alphabetically", so item 10 comes before item 2. For a natural order, put the number in its own column (or pad numbers with zeros: item 02).
Google Sheets
Use Data → Sort range, or the formulas:
=SORT(A1:A100)
=SORT(A1:A100, 1, FALSE)
=SORT(UNIQUE(A1:A100))
=SORT(A1:A100, RANDARRAY(ROWS(A1:A100)), TRUE)
The last one shuffles the list; it re-shuffles every time the sheet recalculates, so copy and paste values only when you are happy with the order. Sorting by length: =SORTBY does not exist in Sheets, but =SORT(A1:A100, LEN(A1:A100), TRUE) works.
Notepad++ and VS Code
- Notepad++: select the lines and use Edit → Line Operations → Sort Lines Lexicographically Ascending. The same menu has Sort Lines As Integers, Sort Lines Case Insensitively, Remove Duplicate Lines and Randomize Line Order.
- VS Code: select the lines, open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and run Sort Lines Ascending or Sort Lines Descending. There is no built-in shuffle; extensions add it.
Command line
sort input.txt # A → Z
sort -r input.txt # Z → A
sort -f input.txt # ignore case
sort -u input.txt # remove duplicates
sort -V input.txt # natural (version) order: item 2 before item 10
sort -n numbers.txt # numeric
shuf input.txt # random order (GNU coreutils)
awk '{ print length, $0 }' input.txt | sort -n | cut -d' ' -f2- # by length
tail -r input.txt # reverse order on macOS; use tac on Linux
sort uses your locale's collation. LC_ALL=C sort sorts by byte value (uppercase before lowercase, accents last), while LC_ALL=sk_SK.UTF-8 sort applies Slovak rules if that locale is installed. On macOS shuf is available as gshuf after brew install coreutils.
Common pitfalls
- Leading spaces sort before letters, so " zebra" ends up at the top. Trim lines first.
- Uppercase vs. lowercase. Byte-order sorting (
LC_ALL=C, many programming languages) puts all capitals before all lowercase letters:B, a, b. Case-insensitive or locale-aware sorting givesa, B, b. - Numbers as text. "10" sorts before "9" alphabetically. Use natural or numeric sort.
- Empty lines collect at the top. Remove them, or use the empty lines remover.
- Duplicates. Sorting puts duplicates next to each other but does not remove them. Tick "Remove duplicates", or use the duplicate remover if you need to keep the original order.
FAQ
How do I sort lines alphabetically online?
Paste the list into the sort lines tool – A → Z is the default, so the result appears immediately.
How do I shuffle lines randomly?
Click Shuffle lines in the tool. In a terminal use shuf file.txt; in Excel 365 =SORTBY(A1:A100, RANDARRAY(ROWS(A1:A100))).
Why does "item 10" come before "item 2"?
Alphabetical sorting compares character by character, and "1" is smaller than "2". Use natural sort (in the tool) or sort -V in a terminal.
How do I sort and remove duplicates at the same time?
Tick Remove duplicates in the tool, use =SORT(UNIQUE(A1:A100)) in a spreadsheet, or sort -u on the command line.