how to sort vector of struct in c++

include

include

include

struct Person { std::string name; int age; };

bool comparePersons(const Person &a, const Person &b) { return a.age < b.age; }

int main() { std::vector people = {{"Alice", 25}, {"Bob", 20}, {"Charlie", 30}};

std::sort(people.begin(), people.end(), comparePersons);

for (const auto &person : people) { std::cout << person.name << " " << person.age << std::endl; }

return 0; }