Natural Sort Order Explained: Why 10 Comes After 2, Not Before

May 28, 20266 min read

What Is Natural Sort Order?

Natural sort order (also called "human sort" or "alphanumeric sort") arranges items the way people expect, not the way computers default to. The classic example: alphabetical sorting places file10.txt before file2.txt because it compares character by character — and 1 comes before 2. Natural sort recognizes that 10 and 2 are numbers and orders them by value instead.

Alphabetical:  file1.txt, file10.txt, file2.txt, file20.txt
Natural:       file1.txt, file2.txt, file10.txt, file20.txt

The intuition is simple: read the string the way a human would, treating consecutive digit characters as a single numeric value.

Why Standard Sorting Fails

Computer sorting works by comparing character codes. In ASCII and Unicode, 0 through 9 have codes 48 through 57. When a sort algorithm compares file10 and file2, it examines characters one at a time:

  1. f vs f — equal
  2. i vs i — equal
  3. l vs l — equal
  4. e vs e — equal
  5. 1 vs 21 is less, so file10 wins

The algorithm never looks beyond that first digit. From the computer's perspective, it did exactly what was asked. The problem is that what was asked does not match what humans expect.

This matters anywhere users see sorted lists: file explorers, version numbers, chapter titles, product codes with embedded numbers.

How Natural Sort Works

A natural sort algorithm splits each string into alternating text and number tokens, then compares token by token:

StringTokens
file10.txtfile 10 .txt
file2.txtfile 2 .txt

When two tokens are both text, compare alphabetically. When both are numbers, compare numerically. When types differ, text sorts before numbers (or vice versa, depending on convention).

Token-by-Token Comparison

Comparing file10.txt and file2.txt:

  1. file vs file — equal
  2. 10 vs 2 — numeric comparison: 10 > 2, so file2.txt comes first

That is the entire algorithm. The key insight is recognizing digit sequences and treating them as integers.

Common Use Cases

File names. Versioned files like report_q1.pdf, report_q2.pdf, report_q10.pdf look wrong under alphabetical sort. Natural sort puts them in the expected sequence.

Version numbers. While full semver parsing (like 1.2.10 vs 1.2.9) requires additional logic, natural sort handles the simple case where numbers are embedded in strings.

Numbered lists. Tutorials, legal clauses, and outlines often use mixed numbering: Step 1, Step 2, Step 10. Natural sort keeps them in order.

Product SKUs. Codes like PROD-100, PROD-200, PROD-1010 sort correctly when digits are compared by value.

Natural Sort in Programming Languages

Python

Python 3 does not have a built-in natural sort, but the natsort library handles it:

from natsort import natsorted

files = ['file10.txt', 'file2.txt', 'file1.txt']
print(natsorted(files))
# ['file1.txt', 'file2.txt', 'file10.txt']

JavaScript

A simple implementation tokenizes and compares:

function naturalSort(a, b) {
  const ax = [], bx = [];
  a.replace(/(\d+)|(\D+)/g, (_, $1, $2) => { ax.push([$1 || Infinity, $2 || '']) });
  b.replace(/(\d+)|(\D+)/g, (_, $1, $2) => { bx.push([$1 || Infinity, $2 || '']) });

  while (ax.length && bx.length) {
    const an = ax.shift();
    const bn = bx.shift();
    const nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
    if (nn) return nn;
  }
  return ax.length - bx.length;
}

Linux

The sort command supports natural ordering with -V (version sort):

ls | sort -V
# file1.txt
# file2.txt
# file10.txt

Edge Cases to Watch

Leading zeros. file001.txt vs file1.txt — should leading zeros be treated as padding or as significant digits? Most natural sort implementations treat 001 and 1 as equivalent, but some preserve leading zeros as a tiebreaker.

Negative numbers. temp-5.txt vs temp10.txt — deciding whether the hyphen is part of the number or a separator changes the result.

Decimal numbers. v1.10 vs v1.2 — should the dot be a decimal point or a token separator? Most natural sort implementations treat the dot as a separator, splitting 1.10 into tokens 1, ., 10.

Unicode digits. Full-width digits like (U+FF12) may not be recognized by simple regex-based implementations.

Key Takeaways

  • Natural sort compares digit sequences numerically while comparing text alphabetically — matching human expectations
  • Standard alphabetical sort treats digits as characters, so 10 comes before 2
  • The algorithm splits strings into text and number tokens, then compares token by token
  • Leading zeros, negative signs, and decimal points are edge cases where implementations differ
  • Most programming languages require a library or custom function for natural sort

Try It Yourself

Sort any list with natural order using our free List Sorter. Paste your items, select the sorting mode, and get instantly organized results — no code needed.