FixThatApp

Line Sorter Guide: Sort Lists Alphabetically, Numerically, or by Length

Updated March 19, 2026

Sorting lines of text is one of those tasks that sounds simple but hides surprising complexity. Alphabetical order depends on whether comparison is case-sensitive. Numerical sort differs from alphabetical sort. "Natural sort" (where file10 comes after file9, not before file2) is different again. This guide covers the different sorting modes and when to use each.

Types of Sort

Sort typeDescriptionExample
Alphabetical A–ZDictionary order, case-sensitive by defaultapple, banana, cherry
Alphabetical Z–AReverse alphabeticalcherry, banana, apple
Numerical ascendingBy numeric value, not lexicographic1, 2, 10, 20, 100
Numerical descendingHighest value first100, 20, 10, 2, 1
By length (short first)Shorter lines firstcat, bird, banana
By length (long first)Longer lines firstbanana, bird, cat
Natural sortHandles embedded numbers intelligentlyfile1, file2, file10
Random / shuffleRandomise the orderUnpredictable

Lexicographic Sort vs Natural Sort

This is the most common sorting surprise. In lexicographic (alphabetical) order, strings are compared character by character. Since '1' < '2' < '9', but '1' also comes before '2' in '10' vs '2', the string "10" sorts before "2" lexicographically:

// Lexicographic sort (wrong for filenames)
["file1", "file10", "file2", "file20"].sort()
// Result: ["file1", "file10", "file2", "file20"]
// file10 before file2 — NOT what humans expect

// Natural sort (correct for filenames)
["file1", "file10", "file2", "file20"].sort((a, b) =>
  a.localeCompare(b, undefined, {numeric: true})
)
// Result: ["file1", "file2", "file10", "file20"]

Always use natural sort for filenames, version numbers, numbered list items, or any strings that mix text and numbers.

Case Sensitivity in Sorting

In standard ASCII ordering, all uppercase letters (A–Z, codes 65–90) come before all lowercase letters (a–z, codes 97–122). This means a case-sensitive sort puts Banana before apple, which is often not what people expect.

// Case-sensitive sort (default in most languages)
["banana", "Apple", "cherry"].sort()
// Result: ["Apple", "banana", "cherry"]
// Capital A comes before lowercase b

// Case-insensitive sort
["banana", "Apple", "cherry"].sort((a, b) =>
  a.toLowerCase().localeCompare(b.toLowerCase())
)
// Result: ["Apple", "banana", "cherry"]
// Same result here, but Apple sorts with A items, not before all lowercase

Common Use Cases

Alphabetising CSS Property Lists

Many style guides recommend alphabetising CSS properties within each rule. Sorting them makes it easier to check if a property is already defined and prevents duplicates. Some tools like Stylelint can enforce this automatically, but the online sorter works well for a quick one-time cleanup.

Organising Import Statements

In Python and JavaScript, import statements are often sorted alphabetically by convention (and enforced by linters like isort or ESLint import/order). Sorting imports consistently reduces merge conflicts when multiple developers add imports to the same file.

Sorting Configuration Keys

JSON and YAML configuration files are easier to navigate when keys are sorted alphabetically. It also makes diffs cleaner — a new key always appears in the same predictable position.

Creating Ordered Wordlists

SEO keyword lists, dictionary wordlists, and glossaries all benefit from alphabetical sorting. A sorted wordlist is easier to scan, and deduplication works more efficiently on sorted input (adjacent duplicates only need one comparison).

Sort + deduplicate in the right order

If you want both sorted output and no duplicates, sort first and then run duplicate removal — or use sort -u on the command line which does both in one step. If you care about preserving original order while only removing duplicates, see the Duplicate Line Remover guide.

Sort Any List of Lines Instantly

Paste your lines and sort them alphabetically, numerically, by length, or randomly — with case-sensitive or case-insensitive options.

Open the Line Sorter

Sorting in Different Tools

Terminal (Linux / macOS)

sort file.txt             # alphabetical A-Z
sort -r file.txt          # reverse (Z-A)
sort -n file.txt          # numerical sort
sort -rn file.txt         # numerical descending
sort -f file.txt          # case-insensitive
sort -u file.txt          # sort + remove duplicates
sort -V file.txt          # version/natural sort (GNU sort)

Python

lines = open('file.txt').read().splitlines()

# Alphabetical
lines.sort()

# Case-insensitive
lines.sort(key=str.lower)

# By length
lines.sort(key=len)

# Natural sort (requires natsort library)
from natsort import natsorted
lines = natsorted(lines)

How to Use the Line Sorter Tool

  1. Open the Line Sorter
  2. Paste your lines into the input area
  3. Select the sort mode: A–Z, Z–A, numeric, by length, or random
  4. Toggle case-sensitive or case-insensitive as needed
  5. Click Sort and copy the output