I'd like to sort an array of C-style strings but I don't know how to create the comparison function properly.
//the array
char foo[2][4] = { "abc", "cde" };
//the comparison function
bool cmp(char * a, char * b) //<- actually passing parameters causes trouble
{
for (int i = 0; i < 4; i++)
if (a[i] < b[i])
return true;
else if (a[i] > b[i])
return false;
return true;
}
//sorting
std::sort(foo, foo + 2, &cmp);
I'd like this piece of code to be as fast as possible so I don't want to use vectors or structures and pass them as a reference.
Any help will be appreciated.
//edit I apologize for being imprecise. I don't want to re-implement lexicographical sorting because there is an STL function that does it appropriately. The comparison part doesn't really matter. I just wanted to have access to the elements of the strings (in this case 'letters'), do whatever I want to, and influence the std::sort function by returning true or false.
Take a look at the highest rated response: c++ sort with structs I wanted to do the same with an array of C-strings (not structures) - the comparison itself doesn't matter.