A better approach is this:
function ($team1, $team2) {
if ($team1->points != $team2->points) {
return $team1->points > $team2->points ? 1 : -1;
}
$diff1 = $team1->goalsFor - $team1->goalsAgainst;
$diff2 = $team2->goalsFor - $team2->goalsAgainst;
if ($diff1 != $diff2) {
return $diff1 > $diff2 ? 1 : -1;
}
return 0;
}
Or, in Java (using Guava), I'd write it like this:
public int compare(Team team1, Team team2) {
return ComparisonChain.start()
.compare(team1.getPoints(), team2.getPoints)
.compare(team1.getGoalsFor() - team1.getGoalsAgainst(),
team2.getGoalsFor() - team2.getGoalsAgainst())
.result();
}
Clearly, PHP doesn't have ComparisonChain
, but it shouldn't be hard to implement.