One way we could solve this in C++03 is by creating a Function Object called by_age. This struct will expose an operator call overload that takes a person a and a person b and returns whether the age of the first one is less than the age of the second one.
In C++11 and further standards of the language, you can avoid all that boilerplate by using a lambda expression.
You can see in the following example that, as the third argument of std::sort, I’m using this new syntax that creates a Function Object on the spot and expresses a predicate for an action that we need to execute to sort the vector.
In this example, the lambda expression takes two arguments, person a and person b, and returns a comparison between their ages. If we go back, you can see the similarities between the Function Obejct and the lambda expression.
1 2 3 4 5 6
std::vector<person> people{/* ... */}; std::sort(std::begin(people), std::end(people), [](const person& a, const person& b) { return a._age < b._age; });
Lambda expressions allow you to express actions and predicates with minimal boilerplate.
As you can see, this pattern of calling the predicate and then executing an action is repeated, first the name and then the age. Lambda expressions can be used as short local functions that can really help removing code repetition and increasing readability.