I want to use insertion sort to sort a vector containing some classes. Each class has a string name variable like this:
Code:
class myClass {
string name;
...
}
I want to sort the vector by the names contained in the classes. I tried something like this:
Code:
vector<myVector> insertionSort(vector<myVector> v) {
vector<myVector>::const_iterator itr, temp;
int i, size = v.size();
for( i = 1, itr = v.begin(); i < size; i++ ) {
temp = itr;
itr++;
while( itr->getName().compare(temp->getName()) < 0 )
temp->swapName(itr->getName());
}
return v;
}
The code runs but doesn't sort correctly by name. swapName() is simply does this:
Code:
void myClass::swapName(string n) {
name.swap(n);
}
Any help is appreciated.