Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

std::sort orders the elements in a random-access range, from first up to but not including last. It guarantees O(N log N) comparisons in the worst case, but it is not stable: equivalent elements may change relative order. Include <algorithm>; use std::stable_sort if preserving the order of equivalent elements matters.

How to use std::sort

The iterator pair defines a half-open range, [first, last): the first iterator is included and the last is excluded. An empty or single-element range needs no reordering.

#include <algorithm>
#include <vector>

std::vector<int> values{5, 1, 4, 2, 3};
std::sort(values.begin(), values.end());

The ordinary overload sorts according to the default ordering. Since C++20, that ordering is described in terms of std::less{}; before C++20, the reference describes it in terms of operator<. The non-execution-policy overloads are constexpr since C++20. The function also has comparator overloads and execution-policy overloads, the latter introduced in C++17. See the cppreference std::sort reference for the overload details.

Requirements for the range and elements

std::sort needs random-access iterators, so it works with ranges such as arrays and vectors, but not directly with a std::list. Under the documented requirements since C++11, the value type must be ValueSwappable, MoveConstructible, and MoveAssignable. A list provides its own member sort instead.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sorting with a comparator

A comparator returns true when its first argument should come before its second. For example, a descending comparison of integers can use std::greater<>; portable code using that function object should include <functional>.

#include <algorithm>
#include <functional>
#include <vector>

std::vector<int> values{5, 1, 4, 2, 3};
std::sort(values.begin(), values.end(), std::greater<>{});

A lambda is useful when the desired ordering depends on a field or a more specific rule. The comparator must satisfy the Compare requirements: it must consistently impose a strict weak ordering and must not modify the objects it compares. In particular, it must not give contradictory or inconsistent results, such as reporting both comp(a, b) and comp(b, a) as true or changing its result during sorting.

Decide what should happen on ties

If a comparator looks only at one record field, records with equal values in that field are equivalent under that comparison. std::sort may place those records in either relative order. Add a tie-break field when you need a deterministic sequence, or choose std::stable_sort when you need to retain their original relative order.

Complexity and implementation details

For N elements, the standard guarantee is O(N log N) comparisons in the worst case (or comparator applications when a comparator is supplied). The published C++98 wording originally required that bound only on average; LWG 713 retroactively corrected it to apply to the worst case. The cppreference reference notes that libc++ implemented the corrected requirement starting with LLVM 14. That is historical implementation context, not a benchmark of current toolchains.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Implementations commonly use introsort, but the standard does not require a particular internal algorithm. Portable code should depend on the standard’s guarantees rather than an assumed implementation strategy.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the right sorting algorithm

Need Choice Key distinction
Sort a full random-access range; stability is not required std::sort O(N log N) worst-case comparisons; equivalent elements may change relative order.
Keep the original relative order of equivalent elements std::stable_sort Stable; O(N log² N) comparator applications without enough extra memory, or O(N log N) when enough extra memory is available. See cppreference’s std::stable_sort reference.
Sort a std::list list::sort std::sort requires random-access iterators; the list member function is stable. See cppreference’s list sort reference.
Order only a rank or a prefix instead of the whole range std::nth_element or std::partial_sort These are distinct standard algorithms; choose based on whether you need a selected rank or an ordered prefix. See cppreference’s algorithms reference.

In practice, choose based on the iterator category, whether equivalent elements must retain their order, and whether the entire range needs sorting. Whatever option you use, ensure the comparison rule matches the ordering you intend.

Best Value

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.