TRT
Sign in

Core · Module 8 · Lesson 5 of 5

Stability and what libraries use

A stable sort keeps equal elements in their original order, which is what makes sorting by several keys work. Here is which library sorts are stable, what algorithms they really are, and how to write comparators that don't break them.

Pro — members onlyAbout 13 minutes

By the end you can

  • Define stability and use it to sort by more than one key
  • Name the algorithm and the stability of the standard sort in Python, Java, C++, Go, Rust and JavaScript
  • Write key functions and comparators that define a consistent order

Builds on Counting and radix sort.

01 · A question first

You sort a list of people by name, then sort it again by city. In one program, the people in each city are still in alphabetical order. In another, they are scrambled. What is different about the second sort?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Stability
  • Sorting by several keys
  • What your library actually uses
  • Comparators and keys
  • Sorting text for people
  • The practical summary
  • Pseudocode & Multi-Language Implementations (C++, Java, Python)
  • Complexity Derivation, Edge Cases & Common Mistakes

03 · Practice

Something wrong with this lesson? Report a problem