I was given an array like this
$input = array(-1,1,3,-2,2, 3,4,-4);
I need to sort it so that negative integers are in front, and positive integers are in the back, and the relative position should not change. So the output should be
$output = array(-1 ,-2,-4, 1,3,2,3,4);
I tried this with usort, but I could not save the relative positions.
function cmp ($a, $b) { return $a - $b; } usort($input, "cmp"); echo '<pre>', print_r($input), '</pre>'; Array ( [0] => -4 [1] => -2 [2] => -1 [3] => 1 [4] => 2 [5] => 3 [6] => 3 [7] => 4 )
Any thoughts?
source share