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:
- convert each number to its word representation
- 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.
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:
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:
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
andin 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_wordsfunction 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
1to999,999,999range is conceptually simple but operationally large.

