The sort function

Hello sir,
sir apne bola ki agar asending order main agar humko sorting karni hai to humara jo comparator function hoga usme hum ab iska use karege leking
suppose humare elements [1,2,3] honge
or hame desending order main sorting karni hai to humara
comparator fun
bool compare(int a,int b)
{
cout<<“compare “<<a<<” “<<b<<” “<<bool(a>b)<<”\n”;
return a>b;
}

agar isne return true kiya to hum swaping karenge and false kiya to no swaping
[1,2,3]
1>2 ye condition false hai to vo swaping kaise karega ?

If you want this to be in descending order , you reteurn a>b .
1,2,3.
1>2 ? false.
False is returned that is Do not keep as is. Swap!

so the internal function should be
if(!comparatorfun(a,b))
{
//do swap
}
else
{
//do not swap
}
is this right?

The comparator function returns a value, convertible to bool, which tells us whether the passed First arguments should be placed before the second or not.
For example:
You want to sort 1,2,3 in descending order.
You return 1>2 ?
1>2 is false which says No, do not keep first argument(1) before second argument(2).
I hope This clears your doubt

1 Like

Yeah , thank u so much

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.