Jump to content

Sort Teams By Record and Tie Breakers


Mateobus

Recommended Posts

Ok I have teams in arrays and i want to sort them to make a leaderboard.  Here are the array values:

$team['index'] //starting with 1
$team['name']
$team['conference_points']
$team['goal_differential']
$team['goals_for']
$team['goals_against']
$team['name']

The rules for rankings are as follows.  Team with most points is first. If points are the same, the tiebreaker is whoever has the greatest goal differential. If goal diff is the same, its team with the most goals for. How could I do this with PHP?  Thanks for all the help.
Link to comment
Share on other sites

array_multi_sort(
$team['conference_points'],SORT_NUMERIC, SORT_DESC,
$team['goal_differential'],SORT_NUMERIC, SORT_DESC,
$team['goals_for'],SORT_NUMERIC, SORT_DESC,
$team['goals_against'],SORT_NUMERIC, SORT_ASC,
$team['name'],SORT_STRING, SORT_ASC,
$team['index'],SORT_NUMERIC, SORT_ASC
);

I include all teh fields in this multisort as I have tried in the past to just sort on the sub-arrays required but then indices got lost......
Link to comment
Share on other sites

Now I have confused myself.  Should it really be like
$team1['index'] //starting with 1
$team1['name']
$team1['conference_points']
$team1['goal_differential']
$team1['goals_for']
$team1['goals_against']
$team1['name']

$team2['index'] //starting with 1
$team2['name']
$team2['conference_points']
$team2['goal_differential']
$team2['goals_for']
$team2['goals_against']
$team2['name']
.
.
.
Or should i use matrices:
$team['index']['name'] //starting with 1
$team['index']['conference_points']
$team['index']['goal_differential']
$team['index']['goals_for']
$team['index']['goals_against']
$team['index']['name']

Which of these should i do.  I am leaning towards the latter, but how would i print them in a table with echo statements using your array_multi_sort... Thanks again ahead of time

Link to comment
Share on other sites

no what you had initiall is fine... and the multisort will work fine on that
to put it all in a table....

[code]
<table>
<thead>
  <tr>
  <th>Team</th>
  <th>GF</th>
  <th>GA</th>
  <th>GD</th>
  <th>Points</th>
  </tr>
</thead>
<tbody>
<?php
$i = 0;
$stop = count($team['index']);
while ($i < $stop)
{
?>
  <tr>
  <td><?php echo $team['name'][$i]; ?></td>
  <td><?php echo $team['goals_for'][$i]; ?></td>
  <td><?php echo $team['goals_against'][$i]; ?></td>
  <td><?php echo $team['goal_difference'][$i]; ?></td>
  <td><?php echo $team['conference_points'][$i]; ?></td>
  </tr>
<?php
$i++;
}

?>
</tbody>
</table>

[/code]
Link to comment
Share on other sites

Ok, here is the problem, how do i initially put all of the values into arrays, if i don't have a second index array.  For example

for ($i=1; $i<7; $i++){
$team['wins'] = team wins value

}
i will keep overwritng the team wins amount.  these should be different for different teams.  i need to use matrices to store the index value dont i?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.