How to Sort a Vector of Pairs in C++

In this tutorial, we will learn How to Sort a vector of pairs in C++.

Sort the vector of pairs in the Ascending Order in C++

This type of sorting can be done using the sort() function.
sort() function sorts the elements on the first pair of elements basis.

Also, learn: Using vector class without header file in c++

Let us write the code to illustrate the same:

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

using namespace std;

int main() 
{ vector< pair <int,int> > v={ {1,6},{6,6},{2,5},{3,1} };
  cout<<"Vector pairs are:\n";
  for(int i=0; i<v.size(); i++)
   { cout<<v[i].first<<" "<<v[i].second<<endl;
   }

  sort(v.begin(), v.end());                 // sort in ascending order
  cout<<"Sorting in Ascending Order:\n"; 
  for(int i=0; i<v.size(); i++)
   { cout<<v[i].first<<" "<<v[i].second<<endl;
   }
  return 0;
}

Output:

Vector pairs are:                                                                                                               
1 6                                                                                                                             
6 6                                                                                                                             
2 5                                                                                                                             
3 1                                                                                                                             
Sorting in Ascending Order:                                                                                                     
1 6                                                                                                                             
2 5                                                                                                                             
3 1                                                                                                                             
6 6

Explanation:

In this code, first, we declare the vector of pairs then we print them, we use the sort() function to sort the pairs in ascending order.  After that, we print the sorted pairs.

Sort the vector of pairs in the Descending Order in C++

In order to sort the pair of vectors in the descending order, we need to modify the sort() function.
An explicit user-defined function is called in the sort() function.

Let us write the code to illustrate the same:

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

using namespace std;
 
bool sortrev(const pair<int,int> &a,  const pair<int,int> &b) 
{ 
  return (a.first > b.first); 
}

int main() 
{ 
  vector< pair <int,int> > v={ {1,6},{6,6},{2,5},{3,1} };
  cout<<"Vector pairs are:\n";
  for(int i=0; i<v.size(); i++)
   { cout<<v[i].first<<" "<<v[i].second<<endl;
   }
  
  sort(v.begin(), v.end(), sortrev);
  cout << "Sorting in Descending order :\n" ;
  for (int i=0; i<v.size(); i++)
   { cout<<v[i].first<<" "<<v[i].second<<endl;
   }
  return 0;
}

Output:

Vector pairs are:                                                                                                               
1 6                                                                                                                             
6 6                                                                                                                             
2 5                                                                                                                             
3 1                                                                                                                             
Sorting in Descending order :                                                                                                   
6 6                                                                                                                             
3 1                                                                                                                             
2 5                                                                                                                             
1 6

Explanation:

In this code, first, we declare a user-defined function to arrange the pairs in the descending order. we declare a vector of pairs and print them. we use the modified sort() function in it. After that, we print the sorted pairs.

I hope that this tutorial will help you to solve your problem.

 

Leave a Reply

Your email address will not be published. Required fields are marked *