Hello! You need to write a program that sorts the names alphabetically. The beginning is the first big letter. Here is my code:
#include <iostream> #include <fstream> #include <algorithm> #include <string> #include <ctime> using namespace std; bool Compare(string name1, string name2) { string temp1 = ""; string temp2 = ""; bool b = false; for (int i = 0; i < name1.length(); i++) { if ((name1[i] >= 'A')&&(name1[i]<='Z')) { b = true; } if (b) temp1 += name1[i]; } b = false; for (int i = 0; i < name2.length(); i++) { if ((name2[i] >= 'A') && (name2[i] <= 'Z')) { b = true; } if (b) temp2 += name2[i]; } if (temp1 == temp2) return true; int length; if (temp1.length() == temp2.length()) length = temp1.length(); else length = min(temp1.length(), temp2.length()); for (int i = 0; i < length; i++) { if (temp1[i] > temp2[i]) return false; if (temp1[i] < temp2[i]) return true; } return true; } void main() { ifstream fin("NAMES.IN"); string temp; getline(fin, temp); long N = stoi(temp); string *names = new string[N]; for (int i = 0; i < N; i++) getline(fin, names[i]); sort(names, names + N, Compare); for (int i = 0; i < N; i++) cout << names[i] << endl; } It works fine as long as all the names are different. If they are the same, the error "invalid comparator" appears. Please tell me how to write it correctly so that this error does not exist.