-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparator_sort.cpp
66 lines (56 loc) · 1.3 KB
/
comparator_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> a, pair<int, int> b)
{
return a.second < b.second;
}
bool custom_comp(int x, int y)
{
return x < y;
}
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(2);
// acending order
// sort(v.begin(),v.end());
decending order
sort(v.begin(), v.end(), greater<int>());
// for (int i = 0; i < v.size(); i++)
// {
// cout << v[i] << " ";
// }
// cout << endl;
// custom comparator sorting; its represent hoe comparator sort is work??
// for (int i = 0; i < 3; i++)
// {
// for (int j = i+1; j < 3; j++)
// {
// if(!custom_comp(v[i],v[j]))
// swap(v[i],v[j]);
// }
// }
for (int i = 0; i <3; i++)
{
cout << v[i] << " ";
}
cout << endl;
//Vector pair sorting
int n;
cin >> n;
vector<pair<int, int>> v2;
for (int i = 0; i < n; i++)
{
int x, y;
cin >> x >> y;
v2.push_back({x, y});
}
sort(v2.begin(), v2.end(), comp);
// second pair value sort in acending order
for (int i = 0; i < n; i++)
{
cout << v2[i].first << " " << v2[i].second << endl;
}
}