Ninjakreborn Posted August 21, 2012 Share Posted August 21, 2012 I have a list of players like this: Rich Franklin - 19 Erick Silva - B Jake Ellenberger - B Joe Lauzon - F Travis Browne - 9 Rory MacDonald - 0 Alexander Gustafsson - 0 Jon Jones - 0 Hector Lombard - 19 Benson Henderson - 9 The B are bench, the F are flex. The ones with o's or numbers are contracts. The array has a field called contract_type. 1 = Bench. 2 = Contract 3 = Flex. I need this list to order contract first, followed by flex, followed by Bench. I can't seem to get the sort to work properly. <?php function sort_contracts( $a, $b ) { echo $a->contract_type . ' - ' . $b->contract_type . '<br />'; if ($a->contract_type == 2 && $b->contract_type == 1 || $a->contract_type == 2 && $b->contract_type == 3) { return -1; }else { return 1; } // if ($a->contract_type == 1 || $a->contract_type == 3) { return -1; } // if ($a->contract_type == 2 && $b->contract_type == 2) { return 0; } // if ($a->contract_type == 2 && $b->contract_type == 1) { return 1; }else { return -1; } // if ($a->contract_type == 2 && $b->contract_type == 3) { return 1; }else { return -1; } } usort($fig, 'sort_contracts'); ?> The commented out codes are bits/pieces of various implementations i've tried which don't work. I can't do this at the database level (ORDER BY contract_type) because they aren't coming out in the order I want. I don't want them ordered in 1, 2, 3 or 3, 2, 1. I basically want to order them in 2, 3, 1. Any feedback on this is greatly appreciated it. Link to comment https://forums.phpfreaks.com/topic/267375-cant-get-the-sorting-right/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 21, 2012 Share Posted August 21, 2012 You can use a FIELD() statement in a query to produce any custom ORDER BY term - ORDER BY FIELD(your_column, 2,3,1) Link to comment https://forums.phpfreaks.com/topic/267375-cant-get-the-sorting-right/#findComment-1371106 Share on other sites More sharing options...
Ninjakreborn Posted August 21, 2012 Author Share Posted August 21, 2012 Thanks. I appreciate that. Link to comment https://forums.phpfreaks.com/topic/267375-cant-get-the-sorting-right/#findComment-1371109 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.