std::stable_sort
std::stable_sort sorts a random-access range and keeps the relative order of equal elements. It is typically a merge sort: O(n log n) time and, if it can allocate, O(n) extra memory. Use it when you sort by one key and then another.
Detailed Explanation
Stability means: if a appeared before b and they compare equivalent, a still appears before b after the sort. std::sort may swap them. That matters when you sort users by last name and then by first name in two passes, or when you sort events by priority and want equal priorities to stay in arrival order.
The two-pass pattern: first stable_sort by the secondary key, then stable_sort by the primary key. After the second pass, equal primary keys still have their secondary order. If the second pass is sort (unstable), the secondary order is lost. One pass with a comparator that ties on both keys is clearer and does not depend on stability: [](const User& a, const User& b) { return std::tie(a.last, a.first) < std::tie(b.last, b.first); }. Prefer the combined comparator when you control the code. Use stability when the first order came from somewhere you cannot repeat (a file order, a previous query).
stable_sort needs random-access iterators. Complexity is O(n log n) comparisons. If extra memory is available, implementations allocate a buffer and run merge sort. If allocation fails, they may fall back to an O(n log² n) in-place algorithm. std::sort does not allocate a linear buffer.
list::sort is also stable (it is a merge sort on nodes) and does not need random access. For a list, call the member.
C++20 std::ranges::stable_sort takes a range and a projection.
When you do not need stability, sort is the default: less memory, often slightly faster, same Big-O. Do not pay for stable_sort "just in case" on a hot path of distinct keys.
Equal according to the comparator is !comp(a,b) && !comp(b,a). If your comparator uses < on a float, NaNs make the ordering undefined. If you use <=, you have already left the legal comparator set.
Code Examples
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct Event { int priority; std::string name; };
int main() {
std::vector<Event> ev{{2, "a"}, {1, "b"}, {2, "c"}};
std::stable_sort(ev.begin(), ev.end(),
[](const Event& x, const Event& y) {
return x.priority < y.priority;
});
for (const auto& e : ev) std::cout << e.name << ' ';
std::cout << '\n'; // prints: b a c (a before c, same priority)
}#include <algorithm>
#include <tuple>
#include <vector>
#include <string>
struct User { std::string last; std::string first; };
int main() {
std::vector<User> users{{"Lovelace", "Ada"}, {"Hopper", "Grace"}};
std::sort(users.begin(), users.end(), [](const User& a, const User& b) {
return std::tie(a.last, a.first) < std::tie(b.last, b.first);
});
}Common Gotchas
Two-pass sort needs both passes stable
an unstable second pass destroys the first key's order among ties.
Extra O(n) memory is typical
on a huge range where allocation is unacceptable, `sort` or a custom in-place algorithm is the lever, not `stable_sort`.
Same comparator rules as `sort`
`<=` is undefined behaviour.