I am trying to sort students name and their marks. I want to sort mark first then sort string of student names with same marks.
Here is my code so far:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
struct student
{
int mark;
string name;
};
vector <student> s = {
{30, "Mark"},
{14, "Mitch"},
{23, "Hen"},
{30, "Abo"}
};
sort(s.begin(), s.end(), [](const student& a, const student& b) { return (a.mark < b.mark); });
for (const auto& x : s)
cout << x.mark << ", " << x.name << endl;
}
This code outputs as expected (sorts marks):
14, Mitch
23, Hen
30, Mark
30, Abo
However, I also want it to sort name of students with same grades, i.e. Mark and Abo have same marks of 30, therefore Abo should come before Mark (due to the alphabetical order of their names).
Expected output:
14, Mitch
23, Hen
30, Abo
30, Mark