#include
#include
#include
using namespace std;
bool compare(string s, string s1){
if(s[0]==s1[0])
return s.length()>s1.length(); // descending order.
else
return s<s1; // ascending order.
}
int main() {
int n;
cin>>n;
string s;
string str[n];
for(int i=0;i<n;i++){
cin>>s;
str[i]=s;
}
sort(str,str+n,compare);
for(int i=0;i<n;i++){
cout<<str[i]<<endl;
}
}
My test case1 is not working
hello @lovepreet_singh
in ur comparator, logic for checking prefix is wrong .
u are only checking single character.
refer this->

loop is checking for prefix ,if it voilates then we are comparing lexicographically
otherwise
we are comparing with size.