Ninjakreborn Posted August 20, 2012 Share Posted August 20, 2012 I have an array that looks like this. Array ( [0] => stdClass Object ( [id] => 1439 [player_id] => 11 [league_id] => 34 [fighter_id] => 591 [fighterOrder] => 0 [contract_type] => 2 [contract_date] => 2012-02-27 [contract_start_date] => 2012-03-01 [contract_exp_date] => 2012-05-30 [pickup_date] => 0000-00-00 [drop_date] => 0000-00-00 [reserve_date] => 0000-00-00 [active_date] => 0000-00-00 [changed] => 0 [status] => 1 [draft_round] => 0 [playOffBonus] => 15.75 [q1Points] => 0 [q2Points] => 21 [q3Points] => 0 [q4Points] => 0 [total_points] => 21 ) [1] => stdClass Object ( [id] => 3345 [player_id] => 11 [league_id] => 34 [fighter_id] => 191 [fighterOrder] => 0 [contract_type] => 2 [contract_date] => 2012-05-30 [contract_start_date] => 2012-05-30 [contract_exp_date] => 2012-08-28 [pickup_date] => 0000-00-00 [drop_date] => 0000-00-00 [reserve_date] => 0000-00-00 [active_date] => 0000-00-00 [changed] => 0 [status] => 1 [draft_round] => 0 [playOffBonus] => 0 [q1Points] => 0 [q2Points] => 21 [q3Points] => 0 [q4Points] => 0 [total_points] => 21 ) [2] => stdClass Object ( [id] => 3477 [player_id] => 11 [league_id] => 34 [fighter_id] => 579 [fighterOrder] => 0 [contract_type] => 2 [contract_date] => 2012-07-01 [contract_start_date] => 2012-07-01 [contract_exp_date] => 2012-09-29 [pickup_date] => 0000-00-00 [drop_date] => 0000-00-00 [reserve_date] => 0000-00-00 [active_date] => 0000-00-00 [changed] => 0 [status] => 1 [draft_round] => 0 [playOffBonus] => 0 [q1Points] => 0 [q2Points] => 20 [q3Points] => 0 [q4Points] => 0 [total_points] => 20 ) [3] => stdClass Object ( [id] => 1375 [player_id] => 11 [league_id] => 34 [fighter_id] => 52 [fighterOrder] => 2 [contract_type] => 2 [contract_date] => 2012-05-30 [contract_start_date] => 2012-05-30 [contract_exp_date] => 2012-08-28 [pickup_date] => 2012-02-25 [drop_date] => 0000-00-00 [reserve_date] => 0000-00-00 [active_date] => 0000-00-00 [changed] => 0 [status] => 1 [draft_round] => 0 [playOffBonus] => 15 [q1Points] => 0 [q2Points] => 20 [q3Points] => 0 [q4Points] => 0 [total_points] => 20 ) [4] => stdClass Object ( [id] => 3499 [player_id] => 11 [league_id] => 34 [fighter_id] => 29 [fighterOrder] => 0 [contract_type] => 2 [contract_date] => 2012-07-16 [contract_start_date] => 2012-07-16 [contract_exp_date] => 2012-10-14 [pickup_date] => 0000-00-00 [drop_date] => 0000-00-00 [reserve_date] => 0000-00-00 [active_date] => 0000-00-00 [changed] => 0 [status] => 1 [draft_round] => 0 [playOffBonus] => 0 [q1Points] => 0 [q2Points] => 16 [q3Points] => 0 [q4Points] => 0 [total_points] => 16 ) [5] => stdClass Object ( [id] => 3352 [player_id] => 11 [league_id] => 34 [fighter_id] => 609 [fighterOrder] => 0 [contract_type] => 2 [contract_date] => 2012-05-30 [contract_start_date] => 2012-05-30 [contract_exp_date] => 2012-08-28 [pickup_date] => 0000-00-00 [drop_date] => 0000-00-00 [reserve_date] => 0000-00-00 [active_date] => 0000-00-00 [changed] => 0 [status] => 1 [draft_round] => 0 [playOffBonus] => 0 [q1Points] => 0 [q2Points] => 12 [q3Points] => 0 [q4Points] => 0 [total_points] => 12 ) [6] => stdClass Object ( [id] => 3665 [player_id] => 11 [league_id] => 34 [fighter_id] => 361 [fighterOrder] => 0 [contract_type] => 1 [contract_date] => 0000-00-00 [contract_start_date] => [contract_exp_date] => 0000-00-00 [pickup_date] => 0000-00-00 [drop_date] => 0000-00-00 [reserve_date] => 0000-00-00 [active_date] => 0000-00-00 [changed] => 0 [status] => 1 [draft_round] => 0 [playOffBonus] => 0 [q1Points] => 0 [q2Points] => 0 [q3Points] => 0 [q4Points] => 0 [total_points] => 0 ) [7] => stdClass Object ( [id] => 3514 [player_id] => 11 [league_id] => 34 [fighter_id] => 318 [fighterOrder] => 0 [contract_type] => 2 [contract_date] => 2012-07-25 [contract_start_date] => 2012-07-25 [contract_exp_date] => 2012-10-23 [pickup_date] => 0000-00-00 [drop_date] => 0000-00-00 [reserve_date] => 0000-00-00 [active_date] => 0000-00-00 [changed] => 0 [status] => 1 [draft_round] => 0 [playOffBonus] => 0 [q1Points] => 0 [q2Points] => 0 [q3Points] => 0 [q4Points] => 0 [total_points] => 0 ) ) Then I have sort code that looks like this: function cmp( $a, $b ) { if( $a->total_points == $b->total_points ){ return 0 ; } return ($a->total_points > $b->total_points) ? -1 : 1; } usort($query,'cmp'); This works fine for the total points. Sorts them. But what if I wanted to sort by both. I want all contract types of 2 at the top (sorted by the total points) then the contract points 1 and 3 at the bottom in whatever order. Is this possible without going to great lengths? Quote Link to comment https://forums.phpfreaks.com/topic/267338-array-second-sort-parameter/ Share on other sites More sharing options...
Barand Posted August 20, 2012 Share Posted August 20, 2012 try function cmp($a, $b) { if ($a['contract_type'] == $b['contract_type']) { return $b['total_points'] - $a['total_points']; } else { return ($a['contract_type']==2) ? -1 : 1; } } Quote Link to comment https://forums.phpfreaks.com/topic/267338-array-second-sort-parameter/#findComment-1370886 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.