Home C++ Lists Functions sort() C++ std::list sort() Function Definition In C++, std::list::sort() puts the elements of a std::list in order. By default the smallest element comes first. You can give your own comparison function for another order.
A list has no random access, so the std::sort() function from <algorithm> does not work on it. That is why the list has its own member sort(). It is stable : equal elements keep their old order. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list . Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax Call sort with empty brackets for ascending order, or give a comparison as the argument.
l.sort(); // ascending, uses <
l.sort(std::greater<int>()); // descending
l.sort([](const P& a, const P& b) { return a.age < b.age; }); Call What it does l.sort()Sorts in ascending order with operator<. l.sort(comp)Sorts with your comparison. comp(a, b) returns true when a must come before b.
Do not write std::sort(l.begin(), l.end()) for a list. It needs random access iterators, and a list only has bidirectional ones, so it does not compile.
Library declarations void sort();
template<class Compare>
void sort(Compare comp); Parameters Parameter Description compOptional. A function, lambda, or function object. comp(a, b) returns true if a should be placed before b. Without it, operator< is used.
Return Value Return Value Description Nothing sort() returns void. The list is sorted in place.
Important
list::sort() is stable and takes O(n log n) time. It relinks nodes, so no element is copied, and all iterators and references stay valid. They keep pointing at the same elements, which now sit in new positions.
C++ Examples 1. Sort ascending 2. Sort descending 3. Stable sort by a key Example 1 — sort() Puts the List in Order The default order is from the smallest to the biggest.
#include <iostream>
#include <list>
int main() {
std::list<int> l = {4, 1, 3, 2};
l.sort();
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
} How It Works The list is 4 1 3 2. A list cannot use std::sort(), so we call the member function. sort() compares elements with < and relinks the nodes until the smallest value is first.The loop prints 1 2 3 4. The values were not copied, only the links changed. Example 2 — sort() with std::greater Pass a comparison to reverse the order, so the biggest number is first.
#include <functional>
#include <iostream>
#include <list>
int main() {
std::list<int> l = {4, 1, 3, 2};
l.sort(std::greater<int>());
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
} How It Works std::greater<int>() is a ready-made comparison that says a comes before b when a is bigger .sort() uses it instead of <, so big numbers move to the front.The result is 4 3 2 1. For a different rule, write your own lambda instead of std::greater. Example 3 — sort() Is Stable Sort pairs by their number only. Pairs with the same number keep the order they had before.
#include <iostream>
#include <list>
#include <utility>
int main() {
std::list<std::pair<int, char>> l = {{2, 'a'}, {1, 'b'}, {2, 'c'}, {1, 'd'}};
l.sort([](const std::pair<int, char>& x, const std::pair<int, char>& y) {
return x.first < y.first;
});
for (const auto& p : l) {
std::cout << p.first << p.second << ' ';
}
std::cout << '\n';
return 0;
} How It Works The lambda compares only first, the number. The letters are ignored by the sort. 2a and 2c are equal for this lambda, and so are 1b and 1d.Because list::sort() is stable, equal pairs keep their old order: b before d, and a before c. Frequently Asked Questions How do I sort a std::list in C++? Call the member function l.sort(). It sorts in ascending order. Pass a comparison, such as std::greater<int>(), to change the order.
Why can I not use std::sort() on a list? std::sort() needs random access iterators so it can jump to any element. A list only has bidirectional iterators, so the list provides its own sort().
Is list::sort() stable? Yes. Elements that compare equal keep their original relative order. std::sort() does not promise this.
How fast is list::sort()? It takes O(n log n) time. It relinks nodes and does not copy elements, so it works even for types that cannot be copied.
Does sort() invalidate iterators? No. Iterators and references stay valid and keep pointing at the same elements, but those elements move to new positions in the list.
Next: splice() Move nodes from another list into this list without copying with std::list.
splice() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion