bool comp(int a,int b){
if ((a > 0 && b > 0) || (a < 0 && b < 0))
return false;
if ((a > 0) && (b < 0))
Your assertions are incorrect on two counts:
std::sort
is not guaranteed to preserve the order of elements that don't need sorting, if you get my meaning.
The behaviour of your comp
function is undefined as it doesn't have an explicit return
value on all control paths.
One remedy is to use std::stable_sort
which will preserve the order of elements as best it can. The comparator function can be adjusted such that it's true
if the first argument is negative and the second is positive (let's define 0 to be positive):
bool comp(int a, int b){
return a < 0 && b >= 0;
}
Another remedy is to use std::stable_partition
.