Advertisement
maycod23

comparator_concept

Jun 28th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool cmp(pair<int, pair<string, int>>& x, pair<int, pair<string, int>>& y) {
  5. int marks1 = x.first, age1 = x.second.second, marks2 = y.first, age2 = y.second.second;
  6. string name1 = x.second.first, name2 = y.second.first;
  7.  
  8. if (marks1 != marks2) return (marks1 > marks2);
  9. else if ((marks1 == marks2) && (age1 != age2)) return (age1 < age2);
  10. else return (name1 < name2);
  11. }
  12.  
  13. int main() {
  14.  
  15. //st marks -int|| st name -string|| st age-int
  16. int n; cin >> n;
  17. vector<pair<int, pair<string, int>>> v(n);
  18. for (int i = 0; i < n; i++) {
  19. int marks, age; string name;
  20. cin >> marks >> name >> age;
  21. v[i].first = marks;
  22. v[i].second.first = name;
  23. v[i].second.second = age;
  24. }
  25. sort(v.begin(), v.end(), cmp);
  26. for (int i = 0; i < n; i++) {
  27. cout << v[i].first << " " << v[i].second.first << " " << v[i].second.second << " " << endl;
  28. }
  29.  
  30. //in case of multiple fields we will be doing sorting on the basis of first and then on second and so on,
  31. //in case of tie breakers
  32.  
  33.  
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement