I have always used this method for accomplishing this kind of sort, but doesn't look correct to me
usort($array, function($team1, $team2){
$t1 = ($team1->points * 1000) + ($team1->goalsFor - $team1->goalsAgainst);
$t2 = ($team2->points * 1000) + ($team2->goalsFor - $team2->goalsAgainst);
if($t1 == $t2) return 0;
return $t1 > $t2 ? 1 : -1;
});
an alternative to this method is to use str_pad, which does nearly the same
basically what I do is to separate with zeros the sorting subjects
$t1 = 32008; // 32 points with a GD of 8
$t2 = 32003; // 32 points with a GD of 3
but what if a team got a weird goals difference?
$t1 = 32008; // 32 points with a GD of 8
$t2 = 33000; // 32 points with a GD of 1000
clearly this can't happen, but it's an example
is this a good way? how about the 32-64 bit / floating numbers [im]precision limits?
does someone have suggestions for this?
thank you :)
feel free to improve the title