Here the method is not usort() , if we accept zero, it does not matter ...
<?php $arr = array(-3, -4, 1, -1, 2, 4, -2, 3); $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($positive, $negative); print_r($sorted); ?>
EDIT: no PHP 5.3? Use create_function() as you say:
$positive = array_filter($arr, create_function('$x', 'return $x > 0;')); $negative = array_filter($arr, create_function('$x', 'return $x < 0;'));
source share