jeudi 17 mars 2016

How overload operator < for sort method for objects?

I have this object, for example

 class Employee{
    int id;
    string name;
    string secName;
    }

My main is :

int main(){
vector<Employee> vec = {Employee(1, "Andy", "Good"), Employee(5, "Adam", "Bad"), Employee(2, "Some", "Employee")}

sort(vec.begin(), vec.end())

}

I know how overload sort method for vector when I have one parameter for sorting. It's like:

 bool operator<(Employee a, Employee b ){
    if (a.name<b.name) return true;
    else return false;
    }

But the point is that I have sort not only on one parameter, but on all. So how should I change overloading method?

I've tried this way, but it doesn't work.

bool operator<(Employee a, Employee b)
{
    if (a.id < b.id)
        if (a.name < b.name)
            if (a.surname<b.surname)return true; else return false;
        else return false;
    else return false;
}

Aucun commentaire:

Enregistrer un commentaire