c++ - How overload operator < for sort method for objects? -


i have object, example

 class employee{     int id;     string name;     string secname;     } 

my main :

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 vector when have 1 parameter sorting. it's like:

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

but point have sort not on 1 parameter, on all. how should change overloading method?

i've tried way, 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; } 

if want sort id, secondarily name , tertiary secname should work:

bool operator<(employee a, employee b) {     if (a.id < b.id) return true;     if (a.id > b.id) return false;     if (a.name < b.name) return true;     if (a.name > b.name) return false;     if (a.secname < b.secname) return true;     return false; } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -