fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <tuple>
  5. #include <vector>
  6.  
  7. struct Person
  8. {
  9. std::string firstName;
  10. std::string lastName;
  11. int age;
  12.  
  13. bool operator<(const Person &other) const
  14. {
  15. return std::tie(lastName, firstName, age)
  16. < std::tie(other.lastName, other.firstName, other.age);
  17. }
  18. };
  19.  
  20. void print(const std::string& title, const std::vector<Person> &people)
  21. {
  22. std::cout << title << "\n";
  23. for (const Person &person : people)
  24. {
  25. std::cout << person.lastName << ", "
  26. << person.firstName << ", "
  27. << person.age << "\n";
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. std::vector<Person> people {
  34. { "Bill", "Gates", 44 },
  35. { "Gates", "Bill", 44 },
  36. { "Back", "Gates", 43 },
  37. { "Front", "Gates", 44 },
  38. { "Bill", "Gates", 43 },
  39. { "Gates", "Bill", 43 },
  40. { "Back", "Gates", 44 },
  41. { "Front", "Gates", 43 },
  42. };
  43.  
  44. print("Before Sort:", people);
  45. std::sort(people.begin(), people.end());
  46. print("After Sort:", people);
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 5648KB
stdin
Standard input is empty
stdout
Before Sort:
Gates, Bill, 44
Bill, Gates, 44
Gates, Back, 43
Gates, Front, 44
Gates, Bill, 43
Bill, Gates, 43
Gates, Back, 44
Gates, Front, 43
After Sort:
Bill, Gates, 43
Bill, Gates, 44
Gates, Back, 43
Gates, Back, 44
Gates, Bill, 43
Gates, Bill, 44
Gates, Front, 43
Gates, Front, 44