I have an array of Person objects that contain a member variable, name. I am trying to order them alphabetically using an overloaded comparison operator that uses string::compare to compare the strings in the objects.
bool Person::operator==(Person p) {
if (name.compare(p.name) == 0) {
return true;
}
else {
return false;
}
}
this works and gives me the correct result but I don't know how I can use this to order the names in the array alphabetically.
I have looked around and seen that documentation on string::compare says
relation between compared string and comparing string
- 0 They compare equal
- <0 Either the value of the first character that does not match is lower in > the compared string, or all compared characters match but the compared string > is shorter.
- ,>0 Either the value of the first character that does not match is greater >in the compared string, or all compared characters match but the compared string is longer.
I can't figure out a way to solve my problem with this information though.
Am I on the right track for what I'm trying to do or is there a better way?