I'm trying to figure out the best way to sort an array. This is what the array looks like: Array ( [11687] => Array ( [passed] => 2 [failed] => 4 ) [8545] => Array ( [passed] => 1 [failed] => 0 ) [1631] => Array ( [passed] => 12 [failed] => 16 ) [2251] => Array ( [passed] => 1 [failed] => 1 ) ) PHP: I'm trying to sort it by the sum of passed and failed. So the order would end up like this: 1631,11687,2251,8545. Any ideas?
Loop over the values, and create 2 (or 3, if necessary) arrays. One array holding all "passed" values, and one holding all "failed" values. Then use array_multisort() and you're done.
You could use usort() and define a custom sorting function. The only trouble with that is it assigns new array keys. That isn't really a problem if you run it on a copy of your array though. Here is how that would look: <?php $test = Array ( 11687 => Array ( 'passed' => 2, 'failed' => 4 ), 8545 => Array ( 'passed' => 1, 'failed' => 0 ), 1631 => Array ( 'passed' => 12, 'failed' => 16 ), 2251 => Array ( 'passed' => 1, 'failed' => 1 ) ); // Add another key (alongside 'passed' and 'failed') to save the key numbers - usort assigns new keys, removing any existing ones while (list($key, $value) = each($test)) { $test[$key]['key'] = $key; } echo "Before...<br>\n"; reset($test); while (list($key, $value) = each($test)) { echo "$key "; } usort($test, "cmp"); reset($test); echo "<br>After...<br>\n"; while (list($key, $value) = each($test)) { echo $test[$key]['key']." "; } function cmp($a, $b) { if (($a['passed']+$a['failed']) == ($b['passed']+$b['failed'])) { return 0; } return (($a['passed']+$a['failed']) > $b['passed']+$b['failed']) ? -1 : 1; } ?> PHP:
Actually, you can use UASORT instead of USORT and eliminate all that process of having to store the keys and reassign them. I've also made the compare function shorter, and I stored it in a variable using the create_function() method since it's really only a disposable function that you're only going to need to sort the array so no point in having a full function for it. <?php $test = Array ( 11687 => Array ( 'passed' => 2, 'failed' => 4 ), 8545 => Array ( 'passed' => 1, 'failed' => 0 ), 1631 => Array ( 'passed' => 12, 'failed' => 16 ), 2251 => Array ( 'passed' => 1, 'failed' => 1 ) ); print "Unsorted:<br />"; print_r($test); $sort_function = create_function('$a,$b', 'return ($b[\'passed\'] + $b[\'failed\']) - ($a[\'passed\'] + $a[\'failed\']);'); uasort($test, $sort_function); print "<br />Sorted:<br />"; print_r($test); ?> PHP: