It is quite common that one want to sort not only by a single, but multiple properties of an object. This can even be done in a single sorting run. The trick is to use an appropriate compare function.
The compare function tells the sorting algorithm, whether one element comes before or after a particular other element. In your case that means that you should implement a compare function that ranks
aaa1 before aaa2
aaa1 before aaa10
aaa2 before aaa10
...
aaa99 before bbb1
and so forth. To implement such a compare function you should put your two keys into a structure (instead of concattenating them to a string). For example:
struct SortKey
{
string docName;
int age;
};
Then create a vector of your sort keys, something like:
vector<sortkey> keys;
</sortkey>
And finally you need the compare function, which would look like:
bool MyCompFunction (const SortKey& a, const SortKey& b)
{
return a.docName < b.docName || a.docName == b.docName && a.age < b.age;
}
In other words: Element a comes before b if either its docName is smaller than b's, or if the docNames are equal and the a's age is smaller than b's.
Now you can call sort on your vector and by specifying your own compare function:
sort (keys.begin(), keys.end(), MyCompFunction);