I was trying to sort a two vector first based on values of 1st column and then sort the 2nd column of the sorted vector based on the corresponding values of first column. That is first sort 1st column in decreasing order then sort 2nd column in increasing order where corresponding values of 1st column are same . Example if I entered a vector say:
5 8
4 6
3 1
5 2
4 9
5 3
4 7
Then the sorted output should look like:
4 6
4 7
4 9
5 2
5 3
5 8
3 1
This is what I wanted to do. I wrote this piece of code but I don't know what is going wrong. My code:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool sor_col(vector<int> &v1, vector<int> &v2){
if(v1[0]==v2[0]){
return v1[1]<v2[1];
}
return false;
}
bool sort_d(vector<int> &v1,vector<int> &v2){
return v1[0]>v2[0];
}
int main(){
int n,k,x,y;
cin>>n>>k;
vector<vector<int>> v(n);
for(int i=0;i<n;i++){
cin>>x>>y;
v[i].push_back(x);
v[i].push_back(y);
}
sort(v.begin(),v.end(),sort_d);
sort(v.begin(),v.end(),sor_col);
return 0;
}
Can anyone help me figure out what is going on?