Skip to content

Instantly share code, notes, and snippets.

@insaneyilin
Created August 7, 2019 10:51
Show Gist options
  • Save insaneyilin/2f6bf0807223390bf535182c34632e1c to your computer and use it in GitHub Desktop.
Save insaneyilin/2f6bf0807223390bf535182c34632e1c to your computer and use it in GitHub Desktop.
compare struct using std::tie()
struct Student {
int stu_id;
int age;
float grade;
};
std::vector<Student> students;
// fill students vector
// ...
std::sort(std::begin(students), std::end(students),
[](const Student &lhs, const Student &rhs) -> bool {
return std::tie(lhs.grade, lhs.stu_id, lhs.age) <
std::tie(rhs.grade, rhs.stu_id, rhs.age);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment