What is bool compare()?

Working of this function is unclear.

Hello @wtakashish

Computer cannot compare two things by itself. Like if we ask them to compare apple and orange and return the bigger one, it cannot.
using the bool compare() function we tell the computer how to compare two things.
For apple and orange we can say for example: if length of the string “Apple” is greater than length of string “Orange” then return “true” else return “false”

bool compare(string a, string b) {
   if(a.length() > b.length()) {
           return true;
   }
   return false;
}

Now when we pass this compare function (which is known as the comparator function) the computer will sort the strings on the basis of whose length is larger
Ex:- input arr[] = {“apple”, “pineapple”, “orange”};
sort(arr, arr + 3, comapre);

Then output would be
pineapple orange apple

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.