Working of comparator in sort function create mess in my mind how to resolve it

working of comparator in sort function create mess in my mind how to resolve it.

Where are your facing problem?
Please mention what you cannot understand

are you comfortable with hindi?

Yes i am comfortable

bhai comparator use karte wakt condition kaisein return karte hn pta nahinchal rha hain

Jab ascending order mein sort krna hota
Maanlo array elements a1,a2,a3,a4 hai…
Tog ye ascending order mein tab hoga jab a1<=a2<=a3<=a4 satisfy kare
Similarly descending order klie a1>=a2>=a3>=a4

Same comparator mein bhi lagega
If you want descending order sort, custom comaprator will be like:

mycompare(int a,int b)
{
if(a>b) return true;//Return true for the order in which you want your sorting
else return false;//Return false for opposite condition
}

mycompare(int a,intb)
ye do consecutive honge array ke elements jaise a[i],a[i+1]?

Yes you can say that in the original array a will be before b

1 Like

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.