So, in the c++ documentation in the header there is a nice function that lets you sort vectors. I have a class Person
. I have a vector of pointers to objects o
That's so simple:
struct student
{
string name;
string grade;
};
bool cmd(const student & s1, const student & s2)
{
if (s1.name != s2.name) return s1.name < s2.name;
return s1.grade < s2.grade;
}
Then:
vector s;
sort(s.begin(), s.end(), cmd);
Students will be sorted alphabatically. If two students have the same name, they will be ordered using their grade.