Sort on a string that may contain a number
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If a string may contain a number, plain lexicographic sorting is often not what people expect. For example, "file10" comes before "file2" in normal string order because "1" is compared before "2" character by character. The usual fix is natural sorting, where digit runs are compared numerically instead of as raw text.
Why Plain String Sorting Looks Wrong
Standard sorting treats everything as characters:
Output:
That is correct lexicographic order, but it is not what most humans mean by "sort these filenames."
Natural Sorting with a Custom Key
The standard pattern is to split each string into text parts and numeric parts, then convert numeric parts to integers for comparison.
Output:
This works because the sort key for "file10" becomes something like ["file", 10, ""] instead of just "file10".
Handling Strings Without Numbers
The nice part about this approach is that it also works for strings with no digits at all.
Strings without numbers simply produce keys made of text fragments, so they still participate in the same ordering logic.
Sorting by a Possibly Numeric Entire String
Sometimes the string is either fully numeric or fully text, such as:
In that case, you might want a different rule. For example, place numeric strings first in numeric order, then text strings alphabetically:
Output:
This is a slightly different problem than embedded-number sorting, so it helps to decide which behavior you actually want.
Sorting Objects by a String Field
Often you are not sorting raw strings but records that contain a label. The same natural key can be applied to one field.
This is useful for table rows, API data, filenames, and UI lists that carry extra metadata.
Using a Library
If you need natural sorting often, a dedicated library such as natsort can save time and cover more edge cases.
That is often the most readable production solution if external dependencies are acceptable.
Things to Decide Up Front
Natural sorting sounds simple, but there are policy choices:
- Should comparison be case-sensitive
- Should leading zeros matter
- How should negative numbers be handled
- What about decimal points or version strings such as
"v1.10.2"
The regex-based key above handles many common filename-style cases, but more specialized strings may need a custom parser.
Common Pitfalls
One common mistake is assuming built-in string sorting will automatically treat digits as numbers. It will not.
Another issue is writing a key that converts only the first number in the string. That may work for simple names but fail for version-like strings with multiple numeric segments.
Developers also sometimes compare strings case-sensitively without meaning to, which can put uppercase and lowercase values in surprising positions.
Finally, be clear about whether the entire string may be numeric or whether numbers are embedded inside otherwise textual labels. Those are related but different sorting problems.
Summary
- Plain string sorting is lexicographic, not numeric.
- Use a natural-sort key when strings contain embedded numbers.
- Split text into digit and non-digit parts, and convert digit parts to integers.
- If only some strings are entirely numeric, design a key that handles numeric and text cases explicitly.
- For repeated use, a library such as
natsortcan be cleaner than rolling your own every time.

