Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Last active October 28, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/4ee81897968d864fe1928ac231ce8e94 to your computer and use it in GitHub Desktop.
Save fpdjsns/4ee81897968d864fe1928ac231ce8e94 to your computer and use it in GitHub Desktop.
pair descending order using sort function.
#include<iostream>
#include<algorithm>
using namespace std;
bool compare(const pair<int, int>& a, const pair<int, int>& b)
{
//If the first number is same
if (a.first == b.first)
return a.second > b.second; //The second number in Descending order
return a.first > b.first; //The first number of bigger numbers to move forward -> Descending order
}
int main()
{
int N;
pair<int,int> arr[1000];
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> arr[i].first >> arr[i].second;
}
//Descending order
sort(arr, arr + N, compare);
//Print
cout << endl;
for (int i = 0; i < N; i++)
cout << arr[i].first << " " << arr[i].second << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment