How to Sort Vector in C++: 4 Examples

Featured image for the tutorial "How to sort vector in C++" at jquery-az.com

Sorting a vector in C++

  • In C++, you may sort the vector by using the sort() function.
  • This is defined in the <algorithm> header file, so you have to include this in your C++ file.
  • You may sort the vectors in ascending and descending order

Different ways can be used to sort vectors which are shown in the examples below. For example:

  • Lambda expression
  • Using sort function
  • Writing a custom function

The following examples show using different ways of sorting the vectors in C++ with examples.

A C++ program that uses sort() function for vector sorting in Asc order

In this program, we have created an int type vector with random elements.

Then we sorted the vector in ascending order by using the sort function as follows:

#include <iostream>

#include<vector>

#include <algorithm>

using namespace std;


int main()
{

//Creating a vector
vector <int> vect = { 20, 25, 10, 5, 0 };

   // Using for loop to traverse through vector before sorting
   cout << "Vector before sorting \n";

   for (int x : vect)

   cout << x << " ";

//Applying sorting
sort(vect.begin(), vect.end());

   // Using for loop to traverse through vector after sorting

   cout << "\n\nVector after sorting \n";

   for (int x : vect)

   cout << x << " ";

return 0;

}

Output:

CPP-vector-sort

In the above program:

  • A vector is created
  • Sort() function is used with two parameters.
  • Using two parameters (begin and end) with sort vector in ascending order.
  • The vector is displayed before and after sorting.

Sorting vector in descending order program

By using the third parameter in the sort function (greater<int>()), the vector can be sorted in descending order.

Have a look:

#include <iostream>
#include<vector>
#include <algorithm>

using namespace std;

int main()
{

//Creating a vector
vector <int> vect = { 20, 25, 10, 5, 0 };

   // Using for loop to traverse through vector before sorting
   cout << "Vector before sorting \n";

   for (int x : vect)
   cout << x << " ";
//Applying sorting in desc order

sort(vect.begin(), vect.end(),greater<int>());

   // Display vector elements in desc order
   cout << "\n\nVector after sorting \n";

   for (int x : vect)
   cout << x << " ";

return 0;

}

Output:

CPP-sort-desc

The example of char vector sorting in ascending order

Similarly, you may have other types of vectors than int.

In the program below, we are sorting a vector of char type. First in ascending order:

#include <iostream>
#include<vector>
#include <algorithm>

using namespace std;

int main()

{

//Creating a char type vector
vector <char> vect = { 'n','m','a','z','b' };

   // Display vector before sorting
   cout << "Char Vector before sorting \n";

   for (char ch : vect)

   cout << ch << " ";

//Applying sorting

sort(vect.begin(), vect.end());

   // Display vector elements in Asc order
   cout << "\n\nChar Vector after sorting \n";

   for (char i : vect)

   cout << i << " ";
return 0;

}

Output:

CPP-vector-sort-char

Char vector in Descending order

The program below sorts the char array in desc order by using the third parameter:

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

int main()
{

//Creating a char type vector
vector <char> vect = { 'n','m','a','z','b' };

   // Display vector before sorting
   cout << "Char Vector before sorting \n";

   for (char ch : vect)

   cout << ch << " ";

//Applying sorting in desc order for char vector

sort(vect.begin(), vect.end(),greater<int>());

   // Display vector elements in desc order

   cout << "\n\nChar Vector after sorting \n";

   for (char i : vect)

   cout << i << " ";

return 0;

}

Output:

CPP-sort-char-desc

Using sort() function with lambda to sort a vector

  • In this example, we created a character array of five elements.
  • Then we used sort() function with lambda as the third parameter to sort vector in descending order:
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
//Creating a char type vector
vector <char> vect = { 'n','m','a','z','b' };

                cout<<"Vector before sorting : ";
                for (char i : vect)
                {
                  cout<<" "<<i;
                }

                //Sorting the vector in desc order by using lambda function
                sort(vect.begin(),vect.end(), [](char &a, char &b){ return a>b; });
    //Display vector after sorting

                cout<<"\n\nVector after sorting : ";
                for (char i : vect)
                {
                  cout<<" "<<i;
                }
                return 0;
}

Output:

CPP-sort-lambda

You can see, the vector is sorted in descending order.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!