How to sort a list of strings?
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 of strings sounds simple, but the right answer depends on what kind of order you want. Plain lexicographic sorting is easy, but case sensitivity, locale rules, and in-place versus copied sorting all matter in real code. This article uses Python examples to explain the common patterns clearly.
Basic Lexicographic Sorting
In Python, the simplest way to sort a list of strings in place is list.sort().
This sorts the existing list in ascending lexicographic order.
If you want a new sorted list and want to keep the original unchanged, use sorted() instead.
That distinction matters when mutation would surprise callers.
Case Sensitivity Changes the Result
Default string sorting is case-sensitive. Uppercase letters often sort before lowercase letters because of underlying character ordering.
If you want case-insensitive sorting, provide a key function.
This usually gives the result users expect in application-level text handling.
Reverse Order
Descending order is also straightforward.
You can combine reverse=True with a key function if needed.
Sorting by Length or Another Property
Sometimes you do not want alphabetical order at all. You want to sort by string length or some other derived property.
The key argument is what makes Python sorting flexible. You are not limited to default string comparison rules.
Locale-Aware Sorting Is a Different Problem
If strings contain accented characters or language-specific collation rules, basic Unicode code-point ordering may not match human expectations. In those cases, locale-aware sorting is a separate requirement.
A simple standard-library example looks like this:
Locale behavior depends on the environment, so test it in the target runtime rather than assuming every system will sort the same way.
In-Place vs New List
A lot of confusion comes from mixing sort() and sorted().
Use sort() when:
- you want to modify the existing list
- avoiding an extra list allocation is useful
- mutation is acceptable and obvious
Use sorted() when:
- you need the original order preserved
- you are sorting something iterable that is not already a list
- returning a new value is clearer than mutating in place
That API distinction is often more important than the sorting rule itself.
Stability Is Useful Too
Python's sort is stable. If two strings compare equal under the key function, their original relative order is preserved.
That becomes useful when you do multi-step sorts.
Because the sort is stable, earlier ordering decisions can still matter when the new key ties.
Common Pitfalls
- Using default sorting when the real requirement is case-insensitive sorting.
- Calling
list.sort()and forgetting that it mutates the list. - Expecting
sort()to return the sorted list instead ofNone. - Ignoring locale rules when working with user-facing international text.
- Sorting alphabetically when the actual requirement was length, reverse order, or some other custom key.
Summary
- Use
list.sort()to sort a list of strings in place. - Use
sorted()when you want a new sorted list instead of mutating the original. - Pass
key=str.lowerfor common case-insensitive sorting. - Use
reverse=Truefor descending order and custom keys such aslenfor other criteria. - Treat locale-aware string ordering as a separate problem from plain lexicographic sorting.
Related reading
- How to sort a list of strings numerically
- How to sort a List/ArrayList?
- How to Sort a List<T> by a property in the object
- How to sort a list/tuple of lists/tuples by the element at a given index
- How to sort a m x n matrix which has all its m rows sorted and n columns sorted?
- How to sort a stack using only stack operations?
- How to sort a pandas dataFrame by two or more columns?
- How to sort a string list consists of digits and alphabets in Java?

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.