Please explain this question!

Q5. Algorithms STL#5
Given the following code snippet :

bool comp(string s1, string s2)
{
if(s1.length() < s2.length())
return 1;
else if(s1.length() > s2.length())
return 0;
else return s1 < s2;
}

vector< string > data = {“b”, “a”, “c”, “abc”, “bca”, “xy”};
sort(data.begin(), data.end(), comp);
for(string item : data)
cout << item << " ";

Choose the correct output :

xy bca abc c b a

a b c xy abc bca

a b c abc bca xy

b a c xy abc bca

look at compare function
first we check the length of strings
lower length strings will come first
if lengths are equal then we lexicographically sort them

so correct output will be
b) a b c xy abc bca

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.