Others have already demonstrated solutions in C ++ 98/03. In C ++ 11, you can use lambda instead for comparison:
// ascending age: std::sort(people.begin(), people.end(), [](person const &a, person const &b) { return a.age < b.age; }); // descending age: std::sort(people.begin(), people.end(), [](person const &a, person const &b) { return b.age < a.age; });
And in case this happens:
// ascending name: std::sort(people.begin(), people.end(), [](person const &a, person const &b) { return a.name < b.name; }); // descending name: std::sort(people.begin(), people.end(), [](person const &a, person const &b) { return b.name < a.name; });
IMO, Information is too common a name, so I changed it to person . In contrast, listOfPeople places too much emphasis on form rather than content (and, even worse, this is simply wrong, since you really have a vector of people, not a list at all). IMO, in programming it is usually better to use list only to refer to a linked list, and not to linear data structures in general.
source share