So I'm having some trouble figuring out the proper use of list::sort()
in regards to a list of structs. Heres the relevant code:
struct student
{
char firstnm[20],
lastnm[20];
int id,
grade;
};
list<student> sList;
//Irrelevant code...
cout << "Please enter your own name, id, and grade. (Ex: myfirst mylast 0 12)\n";
cin >> data.firstnm >> data.lastnm >> data.id >> data.grade;
sList.push_back(data);
sList.sort();
The problem I'm trying to solve is using sList.sort()
to sort by id
. However, I have no idea how to properly pass it into list::sort()
. Thanks in advance for any help/time!
EDIT: The solution was simply adding this to my struct
bool operator < (const student& cmp) const {
return id < cmp.id;
}