String sort ques


what to write in bool compare function? how to check between bat and batman??

Modified Code

look at modified Code
it is bit simple

i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved
if you have more doubts regarding this feel free to ask

how do we know that in comparators, a>b means it will be arranged in descending order and a<b means it will be arranged ascending order??? i am so confused!

i still dont understand clearly. in this question… we are returning true when b becomes null, what happens after that ??

bool compare(string a, string b) {
    int i=0;
    while(a[i]==b[i])i++;
    if(a[i]=='\0')return false;
    else if(b[i]=='\0')return true;
    return a[i]<b[i];
}

see what we are doing here is
we are iterating on both string till they are equal

in case of a=bat and b=batman
a will be completed first i.e a[i]=0;
in this case we don’t want that bat come first so we return false;

suppose a=apple and b=bat
in this case none of the above condition is true
we came to last line
return a[i]<b[i]
here we check first character of both the string ‘a’ and ‘b’
as a<b true so it return true
which is correct because we want that apple come first to bat

i hope this clear now
if yes hit a like and don’t forgot to mark doubt as resolved :grinning:
if you have more doubts regarding this feel free to ask