We have discussed some of the cases of sorting vector of pairs in below set 1.

Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)

More cases are discussed in this article

Sometimes we require to sort the vector in reverse order. In those instances, rather than first sorting the vector and later using “reverse” function increases time complexity of code. Therefore, to avoid this we sort the vector in descending order directly.**Case 3 : Sorting the vector elements on the basis of first element of pairs in descending order.**

This type of sorting arranges a selected rows of pairs in vector in descending order . This is achieved by using “sort()” and passing iterators of 1D vector as its arguments.

`// C++ program to demonstrate sorting in vector of` `// pair according to 1st element of pair in ` `// descending order` `#include<bits/stdc++.h>` `using` `namespace` `std;`
`int` `main()`
`{` ` ` `// declaring vector of pairs`
` ` `vector< pair <` `int` `,` `int` `> > vect;`
` ` `// initializing 1st and 2nd element of`
` ` `// pairs with array values`
` ` `int` `arr[] = {5, 20, 10, 40 };`
` ` `int` `arr1[] = {30, 60, 20, 50};`
` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);`
` ` `// Entering values in vector of pairs`
` ` `for` `(` `int` `i=0; i<n; i++)`
` ` `vect.push_back( make_pair(arr[i],arr1[i]) );`
` ` `// Printing the original vector(before sort())`
` ` `cout << ` `"The vector before applying sort is:\n"` `;`
` ` `for` `(` `int` `i=0; i<n; i++)`
` ` `{`
` ` `// "first" and "second" are used to access`
` ` `// 1st and 2nd element of pair respectively`
` ` `cout << vect[i].first << ` `" "`
` ` `<< vect[i].second << endl;`
` ` `}`
` ` `// using modified sort() function to sort`
` ` `sort(vect.rbegin(), vect.rend());`
` ` `// Printing the sorted vector(after using sort())`
` ` `cout << ` `"The vector after applying sort is:\n"` `;`
` ` `for` `(` `int` `i=0; i<n; i++)`
` ` `{`
` ` `// "first" and "second" are used to access`
` ` `// 1st and 2nd element of pair respectively`
` ` `cout << vect[i].first << ` `" "`
` ` `<< vect[i].second << endl;`
` ` `}`
` ` `return` `0;`
`}` |

*chevron_right*

*filter_none*

Output:

The vector before applying sort is: 5 30 20 60 10 20 40 50 The vector after applying sort is: 40 50 20 60 10 20 5 30

**Case 4 : Sorting the vector elements on the basis of second element of pairs in descending order.**

These instances can also be handled by modifying “sort()” function and again passing a call to user defined function.

`// C++ program to demonstrate sorting/in vector of` `// pair according to 2nd element of pair in ` `// descending order` `#include<bits/stdc++.h>` `using` `namespace` `std;`
`// Driver function to sort the vector elements by` `// second element of pair in descending order` `bool` `sortbysecdesc(` `const` `pair<` `int` `,` `int` `> &a,`
` ` `const` `pair<` `int` `,` `int` `> &b)`
`{` ` ` `return` `a.second>b.second;`
`}` `int` `main()`
`{` ` ` `// Declaring vector of pairs`
` ` `vector< pair <` `int` `,` `int` `> > vect;`
` ` `// Initializing 1st and 2nd element of`
` ` `// pairs with array values`
` ` `int` `arr[] = {5, 20, 10, 40 };`
` ` `int` `arr1[] = {30, 60, 20, 50};`
` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);`
` ` `// Entering values in vector of pairs `
` ` `for` `(` `int` `i=0; i<n; i++)`
` ` `vect.push_back( make_pair(arr[i],arr1[i]) );`
` ` `// Printing the original vector(before sort())`
` ` `cout << ` `"The vector before sort operation is:\n"` `;`
` ` `for` `(` `int` `i=0; i<n; i++)`
` ` `{`
` ` `// "first" and "second" are used to access`
` ` `// 1st and 2nd element of pair respectively`
` ` `cout << vect[i].first << ` `" "`
` ` `<< vect[i].second << endl;`
` ` `}`
` ` `// using modified sort() function to sort`
` ` `sort(vect.begin(), vect.end(), sortbysecdesc);`
` ` `// Printing the sorted vector(after using sort())`
` ` `cout << ` `"The vector after applying sort operation is:\n"` `;`
` ` `for` `(` `int` `i=0; i<n; i++)`
` ` `{`
` ` `// "first" and "second" are used to access`
` ` `// 1st and 2nd element of pair respectively`
` ` `cout << vect[i].first << ` `" "`
` ` `<< vect[i].second << endl;`
` ` `}`
` ` `return` `0;`
`}` |

*chevron_right*

*filter_none*

Output:

The vector before sort operation is: 5 30 20 60 10 20 40 50 The vector after applying sort operation is: 20 60 40 50 5 30 10 20

This article is contributed by **Manjeet Singh **.If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sort first half in ascending and second half in descending order | Set 2
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Sort first half in ascending and second half in descending order | 1
- Sorting of Vector of Tuple in C++ (Descending Order)
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Sort first k values in ascending order and remaining n-k values in descending order
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- How to sort a Vector in descending order using STL in C++?
- Sort Matrix in alternating ascending and descending order rowwise
- Program to sort string in descending order
- Stable sort for descending order
- Sort prime numbers of an array in descending order
- How to sort an Array in descending order using STL in C++?
- Sort an array in descending order based on the sum of its occurrence
- Sorting of Vector of Tuple in C++ (Ascending Order)
- Descending order in Map and Multimap of C++ STL
- Sort a linked list that is sorted alternating ascending and descending orders?
- Print numbers in descending order along with their frequencies