sorting
alphabetical order
list management
programming
data organization

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.

Practice algorithms

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():

python
1names = ["Zoe", "Ana", "mike", "Bea"]
2
3print(sorted(names))
4
5names.sort()
6print(names)

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:

python
1names = ["Zoe", "Ana", "mike", "Bea"]
2sorted_names = sorted(names, key=str.lower)
3
4print(sorted_names)

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():

python
names.sort(key=str.lower)

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:

python
1import locale
2
3locale.setlocale(locale.LC_COLLATE, "en_CA.UTF-8")
4
5words = ["éclair", "apple", "Zebra", "ångstrom"]
6words.sort(key=locale.strxfrm)
7
8print(words)

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:

python
items = ["file2", "file10", "file1"]
print(sorted(items))

Default sorting produces file1, file10, file2, which is lexicographically correct but not what most people expect. A simple natural-sort key can help:

python
1import re
2
3def natural_key(value: str):
4    return [int(part) if part.isdigit() else part.lower() for part in re.split(r"(\d+)", value)]
5
6items = ["file2", "file10", "file1"]
7print(sorted(items, key=natural_key))

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:

javascript
1const names = ["Zoe", "Ana", "mike", "Bea"];
2
3names.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: "base" }));
4console.log(names);

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.lower when 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.