Because what you wrote above is
not a boolean expression that returns true if the first argument is ordered before the second arguement.
https://en.cppreference.com/w/cpp/algorithm/sort
comp - comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ​true if the first argument is less than (i.e. is ordered before) the second.
The signature of the comparison function should be equivalent to the following:
bool cmp(const Type1 &a, const Type2 &b); |
What you wrote above is equivalent to:
sort(begin(number), end(number), [](int n1, int n2) { return (n2 - n1) != 0; });
The only information that is being extracted from the lambda is whether or not n2 == n1.
It does not work like C-style strcmp functions, which return a negative, 0, or positive value (this is what I was trying to say in my previous post when talking about compare). Similarly, it is different than the C function qsort, which expects a "comparator" function that returns negative, 0, or positive values.
_________________________________________
Furthermore, the function/lambda passed into sort MUST meet the requirements of what C++ calls the "Compare" requirement:
https://en.cppreference.com/w/cpp/named_req/Compare
One of the rules for this is,
If comp(a,b)==true then comp(b,a)==false |
For your function both (4 - 3) != 0 and (3 - 4) != 0 return true, so your function does not have "strict weak ordering" (see link above).