Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

Is Python's sort stable, and why does it matter?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2026
6 min read

Yes. Python's sort is stable: when two elements compare equal, they stay in the same relative order they had in the input. This guarantee applies to both sorted() and list.sort() and is part of the language documentation, so you can rely on it in every Python 3 version.

A Python stable sort matters because it lets you sort by several criteria in steps, keeps results predictable, and avoids surprising reordering of equal items. This article uses Python 3.13.

Table of Contents

  • What a stable sort means
  • Why stability matters
  • Sorting in multiple passes
  • reverse=True and stability
  • How Timsort achieves it
  • A practical example: sorting a report
  • Frequently Asked Questions
    • Is the Python sort function stable?
    • Does reverse=True break stability?
    • Which algorithm does Python use to sort?
    • Is the sort of min() and max() ties also predictable?
  • Summary

What a stable sort means

A sort is stable if elements with equal sort keys keep their original relative order. Imagine four records sorted by grade:

python
text

Ravi appears before Karan in the input and in the output, and Asha stays ahead of Meera. An unstable algorithm would be free to swap them. Python's implementation, called Timsort, is stable by design, so you will always see the output above.

Why stability matters

Equal keys are common in real data: many orders share a date, many people share a city. Without stability, rerunning a sort could shuffle those ties and make a table jump around, or make tests flaky.

Stability also means the input order acts as a built-in tie-breaker. If your list is already sorted by time, sorting it by user keeps each user's items in time order, with no extra work.

Sorting in multiple passes

The most useful consequence is that you can sort by several keys by sorting repeatedly, from the least important key to the most important one. Each later pass preserves the order produced by the earlier ones for equal keys.

python
text

The first pass puts names in alphabetical order. The second pass sorts by score, highest first, and names stay alphabetical within each score because the sort is stable. This is the standard way to mix ascending and descending order for text, where you cannot simply negate the value.

reverse=True and stability

You might expect reverse=True to reverse the final list and therefore flip tied items. It does not. Python keeps equal items in their original order even with reverse=True, as if the comparison were reversed rather than the output.

python
text

a still comes before b. If you instead called sorted(...) and then list.reverse(), the tied items would flip to b, then a. Prefer the reverse argument when you want ties to stay in input order.

How Timsort achieves it

Timsort is a hybrid of merge sort and insertion sort, introduced in Python 2.3 and used for lists ever since. It finds already-ordered runs in the data, extends them with insertion sort, and merges runs while taking equal elements from the left run first. That merge rule is what keeps the order of equal items.

As a bonus, Timsort is very fast on data that is partly sorted: nearly sorted input takes close to O(n) time, and the worst case is O(n log n). The sort needs extra memory for merging, up to about half the list size in the worst case.

Stability applies to any sort done through sorted() and list.sort(). Other tools may differ: for example, NumPy's default np.sort uses an algorithm that is not stable unless you ask for kind="stable".

A practical example: sorting a report

Stability is easiest to see with a table that is already in a useful order. Suppose log entries arrive in time order and you want them grouped by level without losing the time order inside each group.

python
text

Each level group still lists its entries in time order, even though the sort looked only at the level. With an unstable sort you would have to add the time to the key to get the same result. Relying on stability is fine in Python, but adding the second key explicitly makes the intent obvious to readers.

Frequently Asked Questions

Is the Python sort function stable?

Yes. Both sorted() and list.sort() are guaranteed stable, and this is documented behaviour rather than an implementation detail.

Does reverse=True break stability?

No. Equal items keep their original order even when you pass reverse=True.

Which algorithm does Python use to sort?

Timsort, a hybrid of merge sort and insertion sort that is stable and takes advantage of existing order in the data.

Is the sort of min() and max() ties also predictable?

min() and max() return the first item found among equal candidates, so the earliest equal item in the iterable wins.

Summary

Python's sort is stable, so equal elements keep their input order, with or without reverse=True. Use that to sort by several criteria in passes from the least to the most important key, to get repeatable results, and to keep an earlier order as a tie-break. Timsort provides the guarantee and runs close to linear time on partly ordered data.

Share

Similar posts

  • How do you sort with a key function in Python?
  • How do negative indexes work in Python lists?
  • How do you create a list in Python?
  • How do you use guards and class patterns in Python match-case?
  • How does match-case pattern matching work in Python?
  • How do you modify a list while iterating over it in Python?
  • How do you break out of nested loops in Python?
  • How does the else clause on a Python loop work?