#include <iostream>
#include<bits/stdc++.h>
using namespace std;
bool compare (int a, int b)
{
return a>b;
}
int main()
{
int arr[5]= {1,2,5,9,6};
sort (arr,arr+5, compare);
for (auto i: arr)
cout << i << " ";
return 0;
}
The above code is sorted in descending order. I am completely blank on how it is working. I read articles but did not get a good explanation.
Because you are not calling it. You are passing the compare function to std::sort
without calling it, so that std::sort
can call it for you.
It is called by std::sort
(many times), and std::sort
supplies the parameters from your array
Again std::sort
looks at the return value (true or false) and sorts the two numbers accordingly.