You could write something like this:
or more compactly:Code:bool compareFoo(const Foo& f1, const Foo& f2) { if (f1.s1 < f2.s1) { return true; } else if (f2.s1 < f1.s1) { return false; } else { return f1.s2 < f2.s2; } }
Nonetheless, if you add a member variable to the struct, then this comparison function must change accordingly to cater to that.Code:bool compareFoo(const Foo& f1, const Foo& f2) { return f1.s1 < f2.s1 || !(f2.s1 < f1.s1) && f1.s2 < f2.s2; }
Note that this is a comparison function that is used in the sorting. It is not a sort function by itself. Also, if this is comparison is canonical, then you could just overload operator< for Foo.





Reply With Quote