string sorting
programming tutorial
list manipulation
coding techniques
beginner programming

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.

Practice algorithms

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

python
words = ["banana", "apple", "cherry"]
words.sort()
print(words)

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.

python
1words = ["banana", "apple", "cherry"]
2new_words = sorted(words)
3print(words)
4print(new_words)

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.

python
words = ["Banana", "apple", "Cherry"]
print(sorted(words))

If you want case-insensitive sorting, provide a key function.

python
words = ["Banana", "apple", "Cherry"]
print(sorted(words, key=str.lower))

This usually gives the result users expect in application-level text handling.

Reverse Order

Descending order is also straightforward.

python
words = ["banana", "apple", "cherry"]
print(sorted(words, reverse=True))

You can combine reverse=True with a key function if needed.

python
words = ["Banana", "apple", "Cherry"]
print(sorted(words, key=str.lower, reverse=True))

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.

python
words = ["pear", "watermelon", "fig", "apple"]
print(sorted(words, key=len))

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:

python
1import locale
2
3locale.setlocale(locale.LC_COLLATE, "")
4words = ["éclair", "eagle", "zebra"]
5print(sorted(words, key=locale.strxfrm))

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.

python
1words = ["bb", "aa", "ab", "ba"]
2words.sort()
3words.sort(key=len)
4print(words)

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 of None.
  • 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.lower for common case-insensitive sorting.
  • Use reverse=True for descending order and custom keys such as len for other criteria.
  • Treat locale-aware string ordering as a separate problem from plain lexicographic sorting.

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.