Number Sorting
String Manipulation
Algorithm
Data Processing
Programming

Sorting numbers from 1 to 999,999,999 in words as strings

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you sort numbers by their English word forms, you are no longer doing numeric sorting. You are converting each number to text and then sorting lexicographically, just as you would sort ordinary words in a dictionary. The interesting part is generating the word form consistently and then using it as the sort key.

Numeric Order Versus Word Order

Numeric order and word order are different.

For example:

  • numeric order: 1, 2, 11
  • word order: eleven, one, two

That happens because string sorting compares letters, not magnitudes. So the general solution is:

  1. convert each number to its word representation
  2. sort by that representation

A Number-to-Words Function

A reusable conversion function is the foundation. Here is one Python implementation for numbers up to 999,999,999.

python
1ONES = [
2    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
3]
4TEENS = [
5    "ten", "eleven", "twelve", "thirteen", "fourteen",
6    "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
7]
8TENS = [
9    "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
10]
11
12
13def under_thousand(n):
14    parts = []
15    if n >= 100:
16        parts.append(ONES[n // 100])
17        parts.append("hundred")
18        n %= 100
19    if 10 <= n < 20:
20        parts.append(TEENS[n - 10])
21        return " ".join(parts)
22    if n >= 20:
23        parts.append(TENS[n // 10])
24        n %= 10
25    if 0 < n < 10:
26        parts.append(ONES[n])
27    return " ".join(parts)
28
29
30def number_to_words(n):
31    if n == 0:
32        return "zero"
33
34    parts = []
35    millions = n // 1_000_000
36    n %= 1_000_000
37    thousands = n // 1_000
38    n %= 1_000
39
40    if millions:
41        parts.append(under_thousand(millions))
42        parts.append("million")
43    if thousands:
44        parts.append(under_thousand(thousands))
45        parts.append("thousand")
46    if n:
47        parts.append(under_thousand(n))
48
49    return " ".join(parts)
50
51
52print(number_to_words(1))
53print(number_to_words(11))
54print(number_to_words(999_999_999))

This gives you a stable textual representation that can be used for sorting.

Sorting by the Word Form

Once the conversion exists, sorting is easy:

python
1def sort_by_spelling(numbers):
2    return sorted(numbers, key=number_to_words)
3
4
5print(sort_by_spelling([1, 2, 11, 20, 100]))

The returned numbers are still numeric values, but their ordering is based on the words produced by number_to_words.

If you want to inspect the text side by side:

python
numbers = [1, 2, 11, 20, 100]
for n in sort_by_spelling(numbers):
    print(n, "->", number_to_words(n))

What About the Full Range

Sorting every number from 1 to 999,999,999 is conceptually simple but computationally heavy if you actually materialize the entire list and all word forms at once.

That is almost one billion numbers. The real algorithm is still the same, but the scale changes the engineering concerns:

  • generating all word forms takes time
  • storing them all takes a lot of memory
  • you may need streaming, chunking, or external sorting if you truly process the entire range

So for full-range work, the problem becomes partly a systems problem rather than only a string problem.

Normalization Choices Matter

Before sorting, decide on formatting rules such as:

  • lowercase versus capitalized words
  • hyphenated forms such as twenty-one
  • optional words such as and in some English variants

Different wording rules change lexicographic order. For consistent results, pick one representation and use it everywhere.

Common Pitfalls

The biggest mistake is thinking numeric sorting and word sorting should produce similar results. They are different orderings by definition.

Another mistake is generating inconsistent word forms, such as mixing hyphenated and non-hyphenated spellings. Lexicographic sort order depends on exact string representation.

People also underestimate the scale of the full 1 to 999,999,999 range. The sorting logic is easy, but the memory and runtime cost of handling every value is not trivial.

Finally, do not sort the strings and lose the original numbers unless that is what you want. Often the goal is to sort the numbers by a textual key, not to discard the numeric identity.

Summary

  • Sorting numbers by their words means sorting lexicographically on the text representation.
  • The core solution is a reliable number_to_words function plus a sort key.
  • Numeric order and word order are fundamentally different.
  • Formatting choices such as hyphens and casing affect the final sort order.
  • The full 1 to 999,999,999 range is conceptually simple but operationally large.

Course illustration
Course illustration

All Rights Reserved.