I want to use insertion sort to sort a vector containing some classes. Each class has a string name variable like this:
I want to sort the vector by the names contained in the classes. I tried something like this:Code:class myClass { string name; ... }
The code runs but doesn't sort correctly by name. swapName() is simply does 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; }
Any help is appreciated.Code:void myClass::swapName(string n) { name.swap(n); }



LinkBack URL
About LinkBacks


