Algorithm STL - string sort

#include
#include
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;
cin >> n;

string a[n];
if (n <= 100)
{

    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
}
cout << endl;

sort(a, a + n,comparator);

for (int i = 0; i < n; i++)
{
    cout << a[i] << endl;
}

return 0;

}

why my second test case failed

hi @sahilkumar23102003_bbf6ee38349d98a1,
corrected and commented https://ide.codingblocks.com/s/662089

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.