How does stdsort work for list of pairs?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you sort pairs in C++, the default order is lexicographical: compare the first element, then compare the second element only if the first elements are equal. The other important detail is that std::sort only works on random-access iterators, so it applies to containers like std::vector, not to std::list.
Default Pair Ordering
std::pair already defines comparison operators. That is why this works without any custom comparator:
The sorted result is:
That output comes from lexicographical comparison, not from any special pair-specific sorting algorithm.
What Lexicographical Means Here
For two pairs (a1, b1) and (a2, b2):
- compare
a1anda2 - if they differ, that decides the order
- if they are equal, compare
b1andb2
This is similar to sorting words in a dictionary: compare the first significant component first, then use the next component only as a tiebreaker.
That is why sorting pairs is often a great default for key-value style data or coordinate-like records.
Use A Comparator When You Want A Different Rule
If you want to sort primarily by the second element instead, write a comparator:
This changes the sort semantics completely. The default pair ordering is only the default, not a limitation.
std::sort Does Not Work On std::list
The title often says "list of pairs," but if the container is literally std::list, then std::sort is the wrong algorithm because it requires random-access iterators.
For std::list, use the member function:
If you try std::sort(values.begin(), values.end()) on a real std::list, it will fail to compile.
That container distinction is one of the most important parts of the answer.
Stability Is A Separate Question
std::sort is not stable. If two elements compare equal, their original relative order is not guaranteed to be preserved.
If you need stability, use std::stable_sort on a random-access container, or rely on list::sort, which is stable.
That matters when multiple pairs are considered equal under your comparator and you care about their previous order.
Common Pitfalls
One common mistake is expecting the second element of the pair to drive sorting by default. It only acts as a tiebreaker when the first elements are equal.
Another issue is saying "list of pairs" conversationally while actually using std::vector, or the reverse. The container changes which sorting API is valid.
A third problem is writing a comparator that does not define a proper strict weak ordering, which can make std::sort behave unpredictably.
Finally, some developers assume std::sort is stable when it is not.
Summary
- Pairs sort lexicographically by default: first element first, second element as tiebreaker.
- '
std::sortworks on random-access containers such asstd::vector.' - A real
std::listmust uselist::sortinstead ofstd::sort. - Use a custom comparator when you want to sort by the second element or another custom rule.
- Stability depends on the sorting algorithm, not on
std::pairitself. - When container choice changes, revisit whether the sorting API still applies.

