Skip to content

Sort Lines

Sort lines alphabetically or numerically (natural sort).

This tool sorts lines of input text. The sort runs in your browser; nothing leaves your machine. The interesting question is what kind of sort — alphabetical, numeric, and natural sorts produce different orderings of the same input.

Alphabetical sort (the default)

Compare lines character by character using the underlying byte/code-point order:

Input:        Output:
banana        apple
apple         banana
cherry        cherry

For ASCII, byte order matches alphabetical order for English letters. For Unicode (UTF-8), byte-by-byte comparison matches code-point order, which is mostly but not exactly the same as alphabetical:

  • ASCII letters sort uppercase-before-lowercase: Apple < apple < banana.
  • Accented characters sort after unaccented: apple < àpple.
  • Numbers in strings sort lexically: 10 < 2 (because 1 < 2).

For strict English alphabetical, this is fine. For locale-aware sorting (where é should sort like e, where the alphabet differs by language), you need a Unicode collator.

Case-insensitive sort

Input:        Case-sensitive:    Case-insensitive:
APPLE         APPLE              APPLE
apple         Apple              apple
Apple         apple              Apple

Case-sensitive: uppercase letters sort first because A (0x41) < a (0x61) in ASCII.

Case-insensitive: lines are compared after lowercasing. APPLE, Apple, and apple are tied; the sort is otherwise stable (preserves their relative order).

Numeric sort

Input:        Alphabetical:    Numeric:
10            1                1
2             10               2
1             2                10

Alphabetical sort treats 10 as starting with 1, so it sorts before 2. Numeric sort interprets the line as a number.

Most “sort” tools (including Unix sort) default to alphabetical and require -n for numeric. The tool above has no pure numeric mode; its Natural option compares digit runs as numbers, which sorts a list of plain numbers numerically too.

Numeric sort is appropriate when every line is a number (or starts with one). For mixed alphabetical/numeric data, see natural sort.

Natural sort

Input:        Alphabetical:    Natural:
file2         file1            file1
file10        file10           file2
file1         file2            file10

Natural sort handles “files numbered like a human would expect” — the embedded numbers sort numerically while the surrounding text sorts alphabetically. file2 < file10 < file11, the way a human reads the list.

The implementation: identify runs of digits in each string, compare them as numbers (with shorter sequences padded), compare non-digit runs as text. Most “natural sort” libraries follow Ian Griffiths’ algorithm or similar.

The tool above offers natural sort as an option.

Reverse / descending

Any sort mode can be reversed (largest to smallest). Equivalent to running the sort and reversing the output.

Stability

A stable sort preserves the relative order of equal elements. The default sort in modern browsers (V8, JavaScriptCore) is stable. For sorting lines, stability matters when:

  • Lines compare equal under your chosen comparison (case-insensitive comparison ties).
  • You want secondary ordering (sort by name, then by date — sort by date first, then by name; if name-sort is stable, the date order is preserved within name groups).

Almost all sort tools today are stable; if you encounter unstable sort behavior, it’s likely a custom implementation.

Sort with empty lines

Empty lines sort to the top in ASCII (empty string < any non-empty string). Some sorts treat empty as “skip” or “send to bottom”; the tool above treats empty as a valid sort key (sorts to the top).

For data files where empty lines are separators, strip empty lines before sorting.

Whitespace normalization

Input:                 Strict sort:           Trimmed sort:
  apple                  apple                apple
banana                 banana                  apple
  apple                  apple                banana

Strict mode treats leading whitespace as significant; apple sorts after apple (the leading space is sorted). Trimmed mode strips whitespace before comparing — three lines all become apple and are equally ranked.

The output preserves original whitespace by default; offer a “trim” mode to also normalize the output.

Sorting in different scripts

The default byte-order sort works for ASCII. For non-Latin scripts:

  • Cyrillic: Code-point order for Russian/Ukrainian roughly matches alphabetical, with edge cases.
  • Chinese: Code-point order is by Unicode block, which roughly groups by stroke order — not alphabetical in the way Western users expect.
  • Mixed: A list with English and Chinese mixed produces a sort that puts all Chinese before or after all English depending on the byte ranges.

For locale-aware sort, use a Intl.Collator with the user’s locale. The tool above always sorts that way — it uses localeCompare with your browser’s locale — so accented letters sort next to their base letters rather than after z.

Common pitfalls

  • 10 before 2 in alphabetical sort. Use numeric or natural sort.
  • Apple before apple in case-sensitive sort. Use case-insensitive sort.
  • Locale-incorrect sort. German ß sorts in different positions in different locales. For non-English content, use locale-aware sort.
  • Sorting CSV without a CSV-aware tool. A line-based sort sorts the first column lexicographically. For multi-column sort, use a CSV tool.
  • Sorting and losing structure. Sorting a list of records where some lines are headers or separators destroys the structure. Strip or preserve those before sorting.
  • CRLF vs LF line endings. A file with mixed line endings can produce surprising “duplicate” lines. Normalize line endings first.

For removing duplicates after (or before) sorting, see remove duplicate lines. For other text manipulation, find & replace, text reverser.