Open In App

Sorting a Vector in C++

Last Updated : 10 Nov, 2025
Comments
Improve
Suggest changes
121 Likes
Like
Report

Sorting a vector means arranging the elements of vector in ascending order or in descending order or in desired defined order.

Sorting in Ascending Order

The sort() function from <algorithm> sorts a vector efficiently in ascending order by default or in custom order using a comparator. It has a time complexity of O(n log n) and space complexity of O(1).

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v = {1, 4, 3, 2, 5};

    // Sort vector in ascending order
    sort(v.begin(), v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Explanation: This code sorts the vector in ascending order using the default sort() function, which arranges elements from smallest to largest.

Sorting in Descending Order

C++ provides a built-in comparator greater<int>() that can be passed to sort() to arrange elements in descending order, automatically placing larger elements before smaller ones.

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v = {5, 2, 4, 1, 3};

    // Sort vector in descending order
    sort(v.begin(), v.end(), greater<int>());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
5 4 3 2 1 

Explanation: This code sorts a vector of integers in descending order using the built-in greater<int>() comparator; sort() arranges elements from largest to smallest automatically.

Sorting in Custom Order

A custom comparator in C++ is a function or callable that defines how two elements should be compared when sorting, allowing custom order instead of the default ascending (<) order used by sort()).

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Comparator function for descending order
bool comp(int a, int b)
{
    return a > b;
}

int main()
{
    vector<int> v = {5, 2, 4, 1, 3};

    // Sort vector in descending order
    sort(v.begin(), v.end(), comp);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
5 4 3 2 1 

Explanation: This code sorts a vector of integers in descending order using a custom comparator; sort() compares elements with comp and arranges them from largest to smallest.

Stable Sorting

The stable_sort() method is similar to sort() method, but the only difference is that the stable_sort() maintains the order of elements if the elements are equal.

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v = {1, 4, 3, 2, 5};

    // Sort vector in ascending order
    stable_sort(v.begin(), v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Explanation: Here we sort the vector in ascending order using stable_sort() method which maintains the order of elements.

Related Algorithms


Article Tags :

Explore