Home C++ Algorithms Functions sort() C++ std::sort() Function Definition In C++, std::sort() arranges the elements of a range from smallest to largest. It is fast, and it is the first algorithm most programmers learn.
By default it uses <. You can pass a comparison function, such as std::greater<int>() or a lambda, to change the order. The range needs random access iterators, so it works on vectors, arrays and strings.
Syntax Add #include <algorithm>, then call std::sort(). Start with these two lines.
std::sort(v.begin(), v.end());
std::sort(v.begin(), v.end(), std::greater<int>()); Call What it does std::sort(v.begin(), v.end());Sort from smallest to largest. std::sort(v.begin(), v.end(), std::greater<int>());Sort from largest to smallest.
Library declarations template<class RandomIt>
void sort(RandomIt first, RandomIt last);
template<class RandomIt, class Compare>
void sort(RandomIt first, RandomIt last, Compare comp); Parameters Parameter Description first, last Required. The range to sort. comp Optional. A function that returns true when its first argument should come before the second.
Return Value Return Value Description voidNothing is returned. The range is sorted in place.
C++ Examples 1. Sort ascending 2. Sort descending 3. Sort objects Example 1 — Sort ascending Sort five numbers from small to large.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 2, 9, 1, 7};
std::sort(v.begin(), v.end());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works v.begin() and v.end() mark the whole vector.With no third argument, sort() uses <. The numbers end up from smallest to largest. Example 2 — Sort descending Use std::greater to flip the order.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 2, 9, 1, 7};
std::sort(v.begin(), v.end(), std::greater<int>());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works std::greater<int>() is a ready-made comparison that means "bigger comes first".It replaces the default < rule. The vector now reads from 9 down to 1. Example 3 — Sort objects Sort players by their points with a lambda.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct Player {
std::string name;
int points;
};
int main() {
std::vector<Player> players = {{"Asha", 70}, {"Ben", 95}, {"Chen", 82}};
std::sort(players.begin(), players.end(),
[](const Player& a, const Player& b) { return a.points > b.points; });
for (const Player& p : players) {
std::cout << p.name << ' ' << p.points << '\n';
}
return 0;
} How It Works A struct has no built-in order, so we give sort() a rule. The lambda says player a goes first when it has more points. The result is printed from the highest score to the lowest. Frequently Asked Questions What does std::sort() do? In C++, std::sort() sorts the elements of a range into ascending order. Include <algorithm> to use it.
Which header and C++ version do I need for std::sort()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::sort() return? It returns nothing (void). The range is sorted in place.
How do I sort in descending order? Pass std::greater<int>() as the third argument, or write a lambda that returns a > b.
Is std::sort() stable? No. Equal elements may change their relative order. Use std::stable_sort() to keep that order.
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