Jump to content

Can't get the sorting right.


Ninjakreborn

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.