Search code examples
phparray-multisort

array_multisort appears sort is incorrect


I have several arrays containing team data... array_multisort() appears to not be keeping the team data associations. I have something like this:

Teams[$z] = various strings
ConfNum[$z] = various strings
$DivNum[$z] = various strings
$DivWinner[$z] = integer of 1 or 0.
$TeamRank[$z] = integer of the team's rank

for debugging, I use a for loop and print the value of each... so $Teams[$z] - $ConfNum[$z] - $DivNum[$z] ... etc.

I run array_multisort() (I've used this many times in the past) and suddenly the associations are no longer there.

Here's my debugging.

BEFORE array_multisort:

key - Teams - Conf - Div - Rank - DivWin - Series
z:0 -   3 -    2 -   2 -    0  -  1 -      1
z:1 -   10 -   2 -   7 -    8  -  1 -      2
z:2 -   75 -   2 -   2 -    2  -  0 -      3
z:3 -   11 -   2 -   2 -    5  -  0 -      4
z:4 -   55 -   1 -   1 -    1  -  1 -      5
z:5 -   79 -   1 -   6 -    3  -  1 -      6
z:6 -   67 -   1 -   6 -    4  -  0 -      7
z:7 -   4 -    1 -   1 -    6  -  0 -      8

Please note z:1... team 10 should be Conf 2 Div 2 .. etc. I then run my array_multsort:

array_multisort($DivWinner,SORT_DESC,$TeamRank,$Teams,$ConfNum,$DivNum,$SeriesID);

This puts my DivWinners at the top, then sorted by my TeamRank. I include Teams, ConfNum, DivNum, SeriesID as I want to maintain the associations for all those arrays after the sort.

After the sort I get this:

key -  Teams - Conf - Div - Rank - DivWin - Series
z:0 -   3 -    2 -    2 -   0  -   1 -      1
z:1 -   10 -   1 -    1 -   1  -   1 -      2
z:2 -   11 -   1 -    6 -   3  -   1 -      4
z:3 -   4 -    2 -    7 -   8  -   1 -      8
z:4 -   75 -   2 -    2 -   2  -   0 -      3
z:5 -   55 -   1 -    6 -   4  -   0 -      5
z:6 -   79 -   2 -    2 -   5  -   0 -      6
z:7 -   67 -   1 -    1 -   6  -   0 -      7

So now we go and look at team 10 again.

It's now set to Conf 1 Div 1... completely incorrect.

It is a DivWinner (set to 1) so it should rank "higher", however, TeamRank is 8... so of the four DivWinners, it should be the lowest... instead it's ranked 2nd.


Solution

  • I believe you have a bug in the way you're printing out the sorted arrays, here's what I tried and it seems to work.

    <?php
    $Teams = array(3,10,75,11,55,79,67,4);
    $ConfNum = array(2,2,2,2,1,1,1,1);
    $DivNum = array(2,7,2,2,1,6,6,1);
    $DivWinner = array(1,1,0,0,1,1,0,0);
    $rank = array(0,8,2,5,1,3,4,6);
    $series = array(1,2,3,4,5,6,7,8);
    
    array_multisort($DivWinner,SORT_DESC,$Teams,$ConfNum,$DivNum, $rank, $series);
    
    
    ?><table>
    <tr>
    <th>teams</th>
    <th>conf</th>
    <th>div</th>
    <th>rank</th>
    <th>winner</th>
    <th>series</th>
    </tr>
    <tbody>
    <?php for($i = 0;$i < count($Teams); $i++):?>
      <tr>
        <td><?php echo $Teams[$i]?></td>
        <td><?php echo $ConfNum[$i]?></td>
        <td><?php echo $DivNum[$i]?></td>
        <td><?php echo $rank[$i]?></td>
        <td><?php echo $DivWinner[$i]?></td>
        <td><?php echo $series[$i]?></td>
      </tr>
    <?php endfor;?>
    
    teams conf  div rank  winner  series
    3     2     2   0     1       1
    10    2     7   8     1       2
    55    1     1   1     1       5
    79    1     6   3     1       6
    4     1     1   6     0       8
    11    2     2   5     0       4
    67    1     6   4     0       7
    75    2     2   2     0       3
    

    OR maybe your code is bugged somewhere else.