Try the Sort Lines

Sort Orders Explained: Why "file10" Sorts Before "file2" and When It Matters

"file10" sorts before "file2" alphabetically β€” which is correct for strings but wrong for filenames. Here's natural sort vs lexicographic sort, locale-aware collation for multilingual names, multi-column SQL ORDER BY, and why stable vs unstable sort algorithms matter in practice.

By sadiqbd Β· June 10, 2026

Sort Orders Explained: Why "file10" Sorts Before "file2" and When It Matters

The word "file10" sorts before "file2" β€” and this reveals one of the most common sorting bugs in software

Alphabetical sorting compares strings character by character. When the tenth character in "file10" is compared to the second character in "file2," the string "1" (ASCII 49) comes before "2" (ASCII 50) β€” so "file10" sorts before "file2," "file3," and "file9" despite being numerically larger. This is lexicographic sorting, and it's correct for words but wrong for version numbers, file names, and any alphanumeric sequence where embedded numbers should sort numerically.

Understanding when to use lexicographic vs natural vs numeric sort explains a whole category of counterintuitive results in software, spreadsheets, and databases.


The three sort orders and when each applies

Lexicographic (alphabetical) sort: compares strings character by character using character code values. Uppercase letters sort before lowercase (in ASCII: A=65, Z=90, a=97). Numbers embedded in strings sort as digit characters, not as numbers.

Examples of lexicographic order:

10 apples
2 bananas
20 cherries
3 dates

(Because "1" < "2" < "3" numerically, but "10" < "2" as strings because "1" < "2" at the first character)

Natural sort (human-friendly): treats embedded numbers as integers, not digit strings. "file2" sorts before "file10" because 2 < 10.

Natural sort order:

chapter1.txt
chapter2.txt
chapter10.txt
chapter20.txt

Numeric sort: treats the entire string as a number, discarding non-numeric characters. Useful when sorting a list of pure numbers stored as strings (CSV exports, log data).


Locale-aware sorting: the hidden complexity

Alphabetical sort in English assumes a specific collation β€” the ASCII ordering. For other languages, the "correct" alphabetical order depends on locale.

Swedish vs. German letter ordering: In Swedish: Γ„, Γ–, Γ… sort after Z (at the end of the alphabet) In German: Γ„, Γ–, Ü are variants of A, O, U and sort with them (Γ„pfel = Apfel for sorting purposes)

Spanish and French: Traditional Spanish treats "ch" and "ll" as single letters (alphabetically after C and L). Modern Spanish (post-1994 RAE revision) sorts these as two separate letters.

The practical impact: if you sort a list of European customer names in English locale, German customers named "Γ–..." will sort to the end of the list rather than with the O surnames. For a multilingual customer database, locale-aware collation is essential.

SQL ORDER BY and collation:

-- Default (database collation)
SELECT name FROM customers ORDER BY name;

-- Explicit locale-aware collation (PostgreSQL)
SELECT name FROM customers ORDER BY name COLLATE "de-DE-x-icu";

Multi-column sort in SQL and spreadsheets

Single-column sorts are simple. Multi-column sorts introduce the concept of sort keys and tiebreaking:

SQL ORDER BY with multiple columns:

SELECT * FROM employees
ORDER BY department ASC, last_name ASC, first_name ASC;

This sorts primarily by department; within each department, by last name; within the same last name, by first name. The second and third columns only matter when the preceding columns are equal.

Spreadsheet multi-column sort (Excel/Google Sheets):

  • Data β†’ Sort β†’ Add Level
  • Primary sort: Department (Aβ†’Z)
  • Then by: Last Name (Aβ†’Z)
  • Then by: Salary (Largest to Smallest)

The tiebreaking chain: each additional sort key only affects rows that are identical in all preceding keys. If no two rows share the same department and last name, the third key never activates.


Stable vs unstable sort: why the implementation matters

A sort algorithm is stable if equal elements maintain their original relative order after sorting.

Example: Original list:

Alice, Marketing, 50000
Bob, Engineering, 60000
Carol, Marketing, 55000
Dan, Engineering, 60000

Sorted by department (stable):

Bob, Engineering, 60000   ← Bob was before Dan in original
Dan, Engineering, 60000
Alice, Marketing, 50000   ← Alice was before Carol in original
Carol, Marketing, 55000

Sorted by department (unstable β€” order not preserved):

Dan, Engineering, 60000   ← Dan may appear before Bob
Bob, Engineering, 60000
Carol, Marketing, 55000   ← Carol may appear before Alice
Alice, Marketing, 50000

Why stability matters in multi-step sorting: if you sort by salary first, then sort by department, a stable sort of the department step preserves the salary ordering within each department. An unstable sort loses the sub-ordering.

In Python, sorted() and list.sort() are guaranteed stable. JavaScript's Array.prototype.sort() is stable in modern implementations (ES2019 specification). C's qsort() is not guaranteed stable.


Sorting in data analysis workflows

Sorting before grouping: many data processing pipelines sort data before performing group operations. A sorted file allows sequential processing (reading one pass through) rather than random access β€” essential for handling datasets larger than memory.

Spreadsheet sorts for data exploration:

  • Sort by date to identify trends and gaps
  • Sort by value (descending) to quickly identify top/bottom performers
  • Sort by status or category to group similar records for bulk editing
  • Sort by multiple columns to prepare data for pivot tables

The sort-then-deduplicate pattern: Sort a list β†’ compare adjacent items β†’ remove items equal to the previous item. This is O(n log n + n) and is often faster than hash-based deduplication for lists that fit in memory. It's also the basis of how many database deduplication operations work.


How to use the Sort Lines tool on sadiqbd.com

  1. Paste your list β€” one item per line
  2. Select sort order: alphabetical, reverse, numeric, by length, random
  3. Options: case-sensitive/insensitive, preserve/remove duplicates, trim whitespace
  4. Use natural sort for file names, version numbers, or any list with embedded numbers
  5. Copy the sorted output β€” ready to paste back

Frequently Asked Questions

Why does Excel's sort put numbers before letters? By default, Excel sorts numbers before text in an ascending sort (treating numbers as smaller than any text value). The sort behaviour also depends on whether the column is formatted as text or number β€” numbers stored as text sort lexicographically, which causes the "10 before 2" problem. Format the column as Number before sorting to get correct numeric order.

What is Unicode collation and why does it matter? The Unicode Collation Algorithm (UCA) is the international standard for determining the sort order of Unicode text. It handles accented characters, language-specific sorting rules, and case folding in a consistent way across languages. Databases and applications that claim "correct international sorting" typically implement CLDR (Common Locale Data Repository) collation tables derived from UCA.

Is the Sort Lines tool free? Yes β€” completely free, no sign-up required.


Sorting looks like a solved problem until you encounter version numbers, multilingual names, or multi-column tiebreaking. Natural sort, locale-aware collation, and stable sort algorithms are the nuances that make the difference between sorting correctly and just sorting.

Try the Sort Lines tool free at sadiqbd.com β€” sort any list alphabetically, numerically, by length, or in reverse, with natural sort and deduplication options.

Try the related tool:
Open Sort Lines

More Sort Lines articles