I have this following code where I want to sort vector of string according to the last character of the string. I've done the following but the sorting is done by default rules.
Here's the overloading < part:
bool operator<(const string &s1, const string &s2){
return s1.at(s1.size() - 1) < s2.at(s2.size() - 1);
}
This is in main:
vector <string> nameList;
int n;
cin>>n;
while(n--){
string name;
char str[100];
cin>>str;
name += str;
nameList.push_back(name);
}
sort(nameList.begin(), nameList.end());
for(int i = 0; i < nameList.size(); i++)
cout<<nameList.at(i)<<endl;
Code in ideone: LINK