C++ std::sort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
std::sort(v.begin(), v.end());
std::sort(v.begin(), v.end(), std::greater<int>());
CallWhat 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
C++
template<class RandomIt>
void sort(RandomIt first, RandomIt last);

template<class RandomIt, class Compare>
void sort(RandomIt first, RandomIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The range to sort.
compOptional. A function that returns true when its first argument should come before the second.

Return Value

Return ValueDescription
voidNothing is returned. The range is sorted in place.

C++ Examples

Example 1 — Sort ascending

Sort five numbers from small to large.

C++
#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

  1. v.begin() and v.end() mark the whole vector.
  2. With no third argument, sort() uses <.
  3. The numbers end up from smallest to largest.

Example 2 — Sort descending

Use std::greater to flip the order.

C++
#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

  1. std::greater<int>() is a ready-made comparison that means "bigger comes first".
  2. It replaces the default < rule.
  3. The vector now reads from 9 down to 1.

Example 3 — Sort objects

Sort players by their points with a lambda.

C++
#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

  1. A struct has no built-in order, so we give sort() a rule.
  2. The lambda says player a goes first when it has more points.
  3. The result is printed from the highest score to the lowest.

Frequently Asked Questions

In C++, std::sort() sorts the elements of a range into ascending order. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The range is sorted in place.
Pass std::greater<int>() as the third argument, or write a lambda that returns a > b.
No. Equal elements may change their relative order. Use std::stable_sort() to keep that order.

Next: sort_heap()

Turns a heap into a sorted range.

sort_heap() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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