Menu

Text Sorter

Purpose: Sort a list of lines alphabetically or numerically in either direction - handy for lists, names and data cleanup.

Text Sorter: How It Works

Sorting a list looks trivial until the data contains numbers, mixed case, accented characters or leading whitespace. Then the default alphabetical order produces results that are technically correct and obviously wrong — this page explains why, and which option fixes each case.

Why 10 sorts before 9

Standard alphabetical sorting compares character by character. '10' and '9' are compared at the first character: '1' comes before '9', so '10' wins — regardless of the numbers' actual values.

AlphabeticalNatural
file1, file10, file11, file2, file20, file3file1, file2, file3, file10, file11, file20

Natural sort recognises digit runs and compares them numerically, which is what people expect from filenames, version numbers and anything with an embedded index. It is the right default for most human-facing lists.

Case sensitivity

A case-sensitive sort orders by character code, which places every uppercase letter before every lowercase one: Apple, Banana, apple, banana. Case-insensitive sorting produces apple, Apple, banana, Banana, which is nearly always what a person wants for names, tags and titles.

Accents and locale

Sorting is language-dependent, and character codes do not reflect any language's alphabet. In Swedish, å, ä and ö come after z. In German phone books, ä sorts as 'ae'. In Spanish, ñ has its own position after n. A byte-order sort places every accented character after every unaccented one, which is wrong in every one of these languages.

Locale-aware sorting — Intl.Collator in JavaScript, ICU collation in most databases — handles this. If you are sorting names for people who will read them, it matters.

The invisible-whitespace problem

A line with a leading space sorts before everything, because the space character precedes every letter. Data copied from spreadsheets or PDFs routinely carries leading and trailing whitespace, and the result is a sorted list with a handful of entries stranded at the top for no visible reason. Trim before sorting, always.

Stability

A stable sort preserves the original relative order of items that compare equal. This matters when sorting by multiple criteria: to sort by department and then by name, sort by name first and then by department with a stable algorithm, and the name order survives within each department. With an unstable sort it does not, and the result looks arbitrary.

Other useful orderings

OrderUse for
By lengthFinding outliers, truncated entries, empty rows
ReverseMost recent first, descending values
Random / shuffleSampling, fair ordering, removing position bias
By last wordSorting full names by surname

Practical sequence

  1. Trim whitespace.
  2. Decide whether blank lines are removed or kept.
  3. Choose natural ordering if the data contains numbers.
  4. Choose case-insensitive unless case is meaningful.
  5. Deduplicate before or after depending on whether duplicates should be counted.

Sorting runs entirely in your browser here, which matters when the list is customer names, email addresses or internal identifiers — data that should not be pasted into a server-side tool.

Frequently Asked Questions

Why does file10 come before file2?
Alphabetical sorting compares character by character, and '1' precedes '2'. Use natural sort, which recognises runs of digits and compares them as numbers — that is what people expect from filenames and version strings.
Why do some lines end up at the top for no reason?
Almost always leading whitespace. A space sorts before every letter, so entries with an invisible leading space are stranded at the start. Trim the text before sorting.
Should I sort case-sensitively?
Usually not for human-readable lists. Case-sensitive sorting groups all uppercase before all lowercase, so 'Apple, Banana, apple' is the result. Case-insensitive matches expectations for names, tags and titles.
Why are accented characters sorted at the end?
Because a byte-order sort uses character codes, which place accented letters after the unaccented alphabet. Correct ordering is language-specific — Swedish places å, ä, ö after z — and needs locale-aware collation.
What is a stable sort and why does it matter?
A stable sort keeps equal items in their original relative order. It lets you sort by several criteria in sequence — sort by name, then by department — with the earlier ordering preserved inside each group.
How do I sort names by surname?
Sort by the last word of each line. This works for most Western names but fails for compound surnames, names where the family name comes first, and particles like 'van der'. For anything important, store given and family names as separate fields.

Related Text Tools

Browse all Text tools →