After searching on google for some time I was not able to find a answer specifically answering this question. How to reverse sort a c++ STL pair with some custom type in it
pair <long long, pair< int, int > > p[MAX];
After searching on google for some time I was not able to find a answer specifically answering this question. How to reverse sort a c++ STL pair with some custom type in it
pair <long long, pair< int, int > > p[MAX];
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
typedif pair <long long, pair< int, int > > pll;
pll p[MAX];
//custom sort function can be customized
bool SortByWeight(pll x, pll y){
return x.first > y.first;
}
int main(){
//enter size
cin>>szie;
//Ex: p = [(10101, (2,3)), (129334, (4, 7))....]
sort(p, p+size, SortByWeight)
}
Custom function can be customized for custom type. For ex for class we can use use class_obj.variable_name etc
std::pair
provides a member-wise operator <
on its own (as well as operator >
), so all you need to do is ask std::sort
to use operator >
:
std::sort(p, p + MAX, std::greater<pair <long long, pair< int, int > >>());
More documentation here: std::pair, std::sort, std::greater.