This is my array:
$arr = array(-3, -4, 1, -1, 2, 4, -2, 3);
I want to sort it like this:
1
2
3
4
-1
-2
-3
-4
So first there would be values greated than zero sorted from the lowest value to the highest value, then there would be negative values sorted from the highest value to the lowest value.
Is there some elegant way to do开发者_运维技巧 this?
Here's a simple comparison function:
function sorter($a, $b) {
if ($a > 0 && $b > 0) {
return $a - $b;
} else {
return $b - $a;
}
}
$arr = array(-3, -4, 1, -1, 2, 4, -2, 3);
usort($arr, 'sorter');
var_dump($arr);
Aside: With the above, zero falls on the negative side of the fence. Change the >
to >=
if you want them to rise to the top of the positive side of said fence.
Here's a non-usort()
method, assuming zero is irrelevant...
<?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;'));
usort() can sort anything with your own set of rules
dunno if it fits to your aesthetics feelings
I'm sure this can be made shorter but this works:
<?php
function cmp($a, $b)
{
if ($a == $b)
return 0;
if($a>=0 && $b>=0 )
return ($a < $b) ? -1 : 1;
if( $a<=0 && $b<=0)
return (-$a < -$b) ? -1 : 1;
if($a>0)
return -1;
return 1;
}
$a = array(-3, -4, 1, -1, 2, 4, -2, 3);
var_dump($a);
usort($a, "cmp");
var_dump($a);
?>
Working link.
Best algorithm I got into is:
if ((a >= 0) == (b >= 0)) {
return a - b;
} else {
return b - a;
}
That will sort negative numbers from the end of array (like splice)
精彩评论