How to sort a string using custom compare function in C++?

Viewed 995

I need to sort characters of a string and need to have lower letters appear first. For example, "acDbA" would become "abcAD" after sorting.

The code below is what I came up with:

bool compare(const char& c1, const char& c2) {
    if (c1 >= 'A' && c1 <= 'Z' && c2 >= 'a' && c2 <= 'z') return false;
    if (c2 >= 'A' && c2 <= 'Z' && c1 >= 'a' && c1 <= 'z') return true;
    return c1<c2;
}
void sortLetters(string &letters) {
    sort(letters.begin(), letters.end(), compare);
}

However in Visual Studio I got:

'sort': no matching overloaded function found   
'SortLetters::compare': non-standard syntax; use '&' to create a pointer to member  
'void std::sort(_RanIt,_RanIt)': expects 2 arguments - 3 provided   

How should I create custom compare function for sorting a string, I couldn't seem to find examples online with string sorting.

2 Answers
Related