the first sort fucntion sorts the elements of string in lexographic order after compare function is added what is the order and how its getting sorted, Can u help me with the dry run i am unable to understand that.
String-02 sorting
See what our comparator function is doing is , it takes two strings lets name them a & b
Now what it does is, it check length of string a & b, for eg:
a = “Mango”
b = “Orange”
so now it checks the length of the string a(which is 5) & b(which is 6)
so since a.length()!=b.length()
we return maximum of two that is b will come before a
Now see this example
b = “Orange”
c=“Papaya”
length of c & b are same so we go inside if loop
here we will return lexographically smaller string which is b
so order of our strings will be
b c a, that is
Orange Papaya Mango
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.