VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-sort-a-vector-in-descending-order-using-stl-in-c/

⇱ How to Sort a Vector in Descending Order Using STL in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Sort a Vector in Descending Order Using STL in C++?

Last Updated : 11 Jul, 2025

Sorting vector in descending order means arranging the elements in such a way that the first element will be largest, and second element will be second largest and so on.

In C++, the simplest way to sort the vector in descending order is to by using the sort() function with a custom comparator.


Output
9 8 6 2 

Explanation: The std::sort() function sorts a vector in ascending order by default. So, greater<>() function object is used to change the sorting order to descending order.

Using sort() with reverse()

If a custom comparator is not available, just use the simple sort() function to sort the vector in ascending order and then reverse the order using reverse() function.


Output
9 8 6 2 

Using stable_sort() with Custom Comparator

The stable_sort() function is similar to sort() function, but it preserves the relative order of equal elements.


Output
9 8 6 2 

Explanation: The stable_sort() function sorts the vector in ascending order. greater<>() is used as comparator to sort the vector in descending order.

Using Multiset

A C++ multiset stores elements in ascending order by default. So, insert all elements from the vector into the multiset, then copy them back to the vector in reverse order.


Output
9 8 6 2 

Explanation: The multiset rbegin() and rend() return the reverse iterators which are then used by the copy() function to copy all the elements of multiset back to vector in reverse order.

Comment
Article Tags: