String Sorting in Lexicographic order

#include<bits/stdc++.h>
using namespace std;
bool comparator(string s1, string s2)
{
if(s1[0]==s2[0]) // if first character is same, then sort according to length
return s1.length() > s2.length();
else // else sort lexicographically
return s1<s2;
}

int main(){

int n; // number of strings
cin>>n;

string str[n]; // string array

for(int i=0; i<n; i++){ // input strings
cin>>str[i];
}

sort(str, str+n, comparator);

for(int i=0; i<n; i++){ // display sorted strings
cout<<str[i]<<endl;
}

return 0;
}

one test case is getting failed . Please advise.

hi @chellapandiyan you are only checking if first character is same, but more than 1 character can be common

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.