In this article, you will learn the different ways to sort a **vector in C++**. You can sort a vector in both ways i.e. either in ascending order or descending order based upon your use case.

This can be achieved with the help of the inbuilt sort function present within the Algorithm class in C++. Since if you are looking for the answer related to **how do I sort vectors in C++ then the simple answer to it is using the std::sort() function that is explained below.**

## Using std::sort method of Algorithm Class

std::sort method present inside the algorithm library can be used to sort a vector in C++. This method uses merge sort and sometimes quicksort based upon the input to maintain the runtime of sorting for vector to be consistent to ** O(** where n is the number of elements in the vector.

*nlog*n)

Let us see the examples of sorting the vector in C++ in actual code.

#### Sort Vector in Ascending Order

#include<bits/stdc++.h> using namespace std; int main(){ //Initializing a Vector vector<int> vectorToSort = {2,5,7,1,8,9,10,3,4,6}; //Using std::sort method of algorithm //This will sort the vector in ascending order sort(vectorToSort.begin(), vectorToSort.end()); //Printing the Sorted Vector for(int x:vectorToSort) cout<<x<<" "; return 0; }

**Output:**

`Sorted Array List: 1 2 3 4 5 6 7 8 9 10 `

As shown in the above code, the vectors are sorted using std::sort() in which we are providing the length of the vector that needs to be sorted. As in above, we are sorting the vector from beginning to end of its length.

#### Sort Vector in Descending Order

With little modification in the above code, you will be able to get the vector sorted in descending order. Below is the example code that you can follow. All you have to do is to add the** greater<int>()** inside the sort method.

#include<bits/stdc++.h> using namespace std; int main(){ //Initializing a Vector vector<int> vectorToSort = {2,5,7,1,8,9,10,3,4,6}; //Using std::sort method of algorithm //This will sort the vector in descending order sort(vectorToSort.begin(), vectorToSort.end(), greater<int>()); //Printing the Sorted Vector cout<<"Sorted Array List: "; for(int x:vectorToSort) cout<<x<<" "; return 0; }

**Output:**

`Sorted Array List: 10 9 8 7 6 5 4 3 2 1`

## How to Check If Vector is Already Sorted

If you want to optimize your code and then before sorting the vectors in your code you should have a check whether the given vectors are already sorted. This can reduce the runtime for your program if you are dealing with lots of lists that are already sorted.

You can use the `std:is_sorted()`

method present in the Algorithm library of C++.

#include<bits/stdc++.h> using namespace std; int main(){ //Initializing a Vector vector<int> vectorToSort1 = {2,5,7,1,8,9,10,3,4,6}; vector<int> vectorToSort2 = {1,2,3,4,5}; //Verifying if the vector is sorted already or not if(!is_sorted(vectorToSort1.begin(), vectorToSort1.end())) sort(vectorToSort1.begin(), vectorToSort1.end()); //Printing the Sorted Vector cout<<"Sorted Array List: "; for(int x:vectorToSort1) cout<<x<<" "; //Verifying if the vector is sorted already or not if(!is_sorted(vectorToSort2.begin(), vectorToSort2.end())) sort(vectorToSort2.begin(), vectorToSort2.end()); else cout<<endl<<"Already Sorted List: "; for(int x:vectorToSort2) cout<<x<<" "; return 0; }

**Output:**

As you can see in the above code, we are using` is_sorted() `

method to verify that the vector is already sorted or not and then sorting the vector. Hence total runtime to run the above code was 1.467 seconds and if we remove the is_sorted() from the above code the runtime increases to 1.48 seconds.

## Wrap Up

That is all for this post on how to sort a vector in C++. As you saw there are different ways by which you can achieve that in the above code.

Then please follow us on **Facebook **and **Twitter**. Let us know the questions and answer you want to cover in this blog.

**Further Read:**