Sort an integer array, negative integers in the front and positive integers in the back

Try this..

$arr = array(-1,1,3,-2,2, 3,4,-4);


$positive = array_filter($arr, function($x) { return $x > 0; });
$negative = array_filter($arr, function($x) { return $x < 0; });

sort($positive);
rsort($negative);

$sorted = array_merge($negative,$positive);
print_r($sorted);

Demo:https://eval.in/419320

Output:

Array
(
    [0] => -1
    [1] => -2
    [2] => -4
    [3] => 1
    [4] => 2
    [5] => 3
    [6] => 3
    [7] => 4
)

This is an ordering problem, but it's not a sort.

The problem can be broken down as follows:

  1. Separate the given array into two arrays; one of negative numbers, the other of positive numbers. (Should consider where you want 0.) The order of items in each of those arrays should be the same as they appear in the input array.

    Do this by pushing values, for example.

  2. Concatenate the two arrays.