C++
std::sort
pairs
algorithm
programming

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:

cpp
1#include <algorithm>
2#include <iostream>
3#include <utility>
4#include <vector>
5
6int main() {
7    std::vector<std::pair<int, int>> values{{2, 9}, {1, 5}, {2, 3}, {1, 4}};
8
9    std::sort(values.begin(), values.end());
10
11    for (const auto& p : values) {
12        std::cout << '(' << p.first << ", " << p.second << ")\n";
13    }
14}

The sorted result is:

text
1(1, 4)
2(1, 5)
3(2, 3)
4(2, 9)

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 a1 and a2
  • if they differ, that decides the order
  • if they are equal, compare b1 and b2

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:

cpp
1std::sort(values.begin(), values.end(), [](const auto& a, const auto& b) {
2    if (a.second != b.second) {
3        return a.second < b.second;
4    }
5    return a.first < b.first;
6});

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:

cpp
1#include <iostream>
2#include <list>
3#include <utility>
4
5int main() {
6    std::list<std::pair<int, int>> values{{2, 9}, {1, 5}, {2, 3}, {1, 4}};
7
8    values.sort();
9
10    for (const auto& p : values) {
11        std::cout << '(' << p.first << ", " << p.second << ")\n";
12    }
13}

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::sort works on random-access containers such as std::vector.'
  • A real std::list must use list::sort instead of std::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::pair itself.
  • When container choice changes, revisit whether the sorting API still applies.

Course illustration
Course illustration