Algorithmsascii character order
Understanding ASCII Character Order
TT
Testlaa Team
May 14, 2026•1 min read
Characters compare using their code-point order: '0' < '9' < 'A' < 'Z' < 'a' < 'z' in ASCII. That is why sorting strings of digits lexicographically differs from sorting by numeric value ('10' < '2' as strings!).
Why this shows up in the real world
Version strings v9 vs v10 famously need custom parsers because naive string compare lies. Lexicographic file ordering in terminals follows character order byte-by-byte.
Core idea (explained for students)
Python compares strings lexicographically: first differing character decides; if one string is a prefix, shorter comes first. Tie this back to ord values for ASCII subsets.
Try this in Python
pairs = ["10", "2", "1", "20"]
print("lexicographic:", sorted(pairs))
print("numeric:", sorted(pairs, key=int))
Common mistakes
- Sorting numeric strings without padding (
'007'tricks) orintkeys. - Assuming case-insensitive order—default is case-sensitive with uppercase before lowercase in ASCII.
Key takeaways
- When users see “alphabetical order,” clarify ASCII vs locale.
- For interviews, mention both
sorted(list_of_str)and key functions.
Tags:
StringsPythonStudents
