#include <algorithm>
bool comparisonFunc(char* c1, char* c2)
{
return strcmp(c1, c2) ? 0 : 1;
}
vector<char*> myVec;
vector<char*>::iterator itr;
sort(myVec.begin(), myVec.end(), comparisonFunc)
Is that correct or is there a better way to do it?
std::sort
expects a "less than" predicate. You should implement your comparisonFunc()
like this:
bool comparisonFunc(const char *c1, const char *c2)
{
return strcmp(c1, c2) < 0;
}
(Note the const
s; they are important.)
Your current implementation couldn't possibly work because you just return if the values are equal or not. That information is not enough to sort - you need to know which one is smaller and which one is bigger (unless, of course, the values are equal).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With