I have the following array
$postarray[] = array( 'total' => '6.5', 'points' => '300' );
$postarray[] = array( 'total' => '11.4', 'points' => '320' );
$postarray[] = array( 'total' => '6.5', 'points' => '340' );
$postarray[] = array( 'total' => '2.6', 'points' => '300' );
$postarray[] = array( 'total' => '12.8', 'points' => '320' );
And I want to sort it into the following. Notice that if the points
are equal then it compares total
and sorts it in ascending order.
$postarray[] = array( 'total' => '6.5', 'points' => '340' );
$postarray[] = array( 'total' => '12.8', 'points' => '320' );
$postarray[] = array( 'total' => '11.4', 'points' => '320' );
$postarray[] = array( 'total' => '6.5', 'points' => '300' );
$postarray[] = array( 'total' => '2.6', 'points' => '300' );
So far I have used usort
to sort according to the points
but I'm not sure how to sort the total
field too.
function sortByOrder($a, $b) {
if ($b['points'] > $a['points']) {
return $b['points'] - $a['points'];
} elseif ($b['points'] == $a['points']) {
return 0;
}
}
usort($postarray, 'sortByOrder');