Hi, I have an array of strings and I am trying to organize them in alphabetical order. It isn't working. Can someone please tell me if there is a way to do this and if so how. Thanks. I will post the cryptic error message below.
here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void sortList(string unorgList[])
{
string temp;
for (int i = 0; i < 200; i++)
{
for (int j = 1; j < 139; j++)
{
if ( strcmp( unorgList[1], unorgList[2]) < 0)
{
temp = unorgList[1];
unorgList[1] = unorgList[2];
unorgList[1] = temp;
}
}
}
}
error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
Also, the std::sort function works with arrays, you just need to give it the address of the first element and the address of the last element. In this case, however, it would try to sue operator<, which std::string doesn't have, so it won't work in this case. But useful to know for future reference.
My professor said that we have to write our own sort function. I can't think of another way to do it besides comparing string sizes. Does anyone else have any ideas? Thanks!
here the second character is different. In ASCII 'e' is 0x65 and 'a' is 0x61. Since the compared string's unmatching charater has higher value than the comparing string's, the function returns a value greater than 0.