Comparators regarding

how does a comparator work please explain in a detail …really comfused!!!1

like if we have this

bool compare (int a, int b){

return a<b;

}

and if we have this whats difference

bool compare (int a, int b){

return a>b;

}

and what will this return inn which order and why

bool compare (string s1, string s2){

return s1,s2;

}

Hello @talhashamim001 in the first comparator fucntion it will be sorted in the ascending order. i.e
it will compare if the element a is less then b otherwise it will return false.
whereas in the second comparator fucntion it will compare if the first element is greater then the second then it will return true.
third comparator is wrong which you have shared.
if you have any doubt you can ask here:
Happy Learning!!

return s1>s2 in third one

third comparator will make this sorted in the lexicogrpahical fromat.
it will sort on the basis of the first letter and if the first letter is same then it will compare on the basis of the second character.
Happy Learning!!

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.