Sort a list alphabetically
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Sorting a list alphabetically usually means sorting strings in lexicographic order, but real applications often need a more specific rule. Case sensitivity, locale, accents, and embedded numbers can all change what users expect "alphabetical" to mean.
Basic alphabetical sorting in Python
In Python, the two standard tools are sorted() and the list method .sort():
sorted(names) returns a new sorted list, while names.sort() sorts the list in place. The default order is lexicographic and case-sensitive, so uppercase letters usually come before lowercase letters. That is why "Zoe" and "Bea" may appear before "mike" even if a human reader expects case-insensitive ordering.
Sort case-insensitively
If you want a more human-friendly alphabetical result, provide a sort key:
This keeps the original strings but compares them using lowercase versions. That is the most common fix when people say their list is "not sorting alphabetically" even though the language is technically doing exactly what it was asked to do.
The same idea works with .sort():
Sort by locale when language rules matter
Alphabetical order in English is not always the right order for other languages. When locale matters, use locale-aware comparison:
Locale-sensitive ordering is important for user-facing interfaces, reports, and exports. If you skip it, accented characters may land in surprising positions because the sort falls back to raw Unicode code-point comparisons instead of language-aware collation rules.
Handle strings with numbers naturally
Sometimes a list looks alphabetical but contains numbers:
Default sorting produces file1, file10, file2, which is lexicographically correct but not what most people expect. A simple natural-sort key can help:
Now the output is file1, file2, file10, which feels more natural to users.
The same idea in JavaScript
If you are sorting in JavaScript, the same rules apply even though the API is different:
localeCompare is a good default for frontend code because it handles locale-aware string comparison more gracefully than a plain greater-than comparison.
Common Pitfalls
The most common mistake is assuming "alphabetical" automatically means case-insensitive. In many languages, the default sort is case-sensitive unless you provide a key or comparator.
Another common issue is confusing sorted() with .sort() in Python. One returns a new list, the other mutates the existing list.
Locale is another easy source of bugs. If your application supports multiple languages, raw string comparison may produce an order that looks wrong to users even when the code is consistent.
Finally, lists with embedded numbers often need natural sorting rather than plain lexicographic sorting. If the data contains names like item2 and item10, test the output with realistic examples instead of assuming the default order is good enough.
Summary
- Use
sorted()for a new list and.sort()to modify a Python list in place. - Add
key=str.lowerwhen you want case-insensitive alphabetical order. - Use locale-aware sorting for user-facing text in languages with special collation rules.
- Apply a natural-sort key when strings contain embedded numbers.
- Define what "alphabetical" means for your specific data before choosing the sorting rule.
Related reading
- Sort a list by multiple attributes?
- Sort a list of tuples by 2nd item integer value
- Sort a list of two-sided items based on the similarity of consecutive items
- Sort a set of 3-D points in clockwise/counter-clockwise order
- Sort a vector in which the n first elements have been already sorted?
- Sort an array according to the elements of another array
- Sort a single String in Java
- Sort Algorithm - find which chart bar sees different bar

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.