分拣收集2次

Sorting collection 2 times

所以我想对一个集合进行排序(这对我来说没问题)但是如果在此排序结果中 2 个值相等,我想比较其他两个值以确定先进行排序。

例:我有30支球队,他们都有积分,但有些球队的积分可能相同。如果是这样,我想查看每支球队的胜利,并将胜利最多的球队放在胜利较少的球队前面。

rankings = $team_stats->sortByDesc(function($product) { return ($product['nhl_season_w'] * 2) + $product['nhl_season_otl'] });

是啊!球队得分排序,现在我想按胜负排序。

感谢您的帮助!

编辑已解决:

我使用 $key + 1 来确保我的团队 ID 保持不变,这样可以更轻松地从团队排名中获取其他信息:)

只需将胜利点数连接起来即可

$rankings = $team_stats->sortByDesc(function($product) {
     return (($product['nhl_season_w'] * 2) + $product['nhl_season_otl']) . '_' . $procuct['victories'];
});

这会将胜利添加到排序字符串中,并确保当两个产品得分相同时最高的胜利保持在最前面。

创建一个多维数组,点为第一键,胜利为第二键,php将自动按键排序。

<?php 
//each array with (Teams, Points, Wins)
$rank[] = array('Team' => "A", 'Points' => 24, 'Wins' => 11);
$rank[] = array('Team' => "B", 'Points' => 26, 'Wins' => 11);
$rank[] = array('Team' => "C", 'Points' => 25, 'Wins' => 10);
$rank[] = array('Team' => "D", 'Points' => 24, 'Wins' => 12);
$rank[] = array('Team' => "E", 'Points' => 25, 'Wins' => 11);
$rank[] = array('Team' => "F", 'Points' => 27, 'Wins' => 13);

foreach ($rank as $key => $row) {
    $points[$key]  = $row['Points'];
    $wins[$key] = $row['Wins'];
}

array_multisort($points, SORT_DESC, $wins, SORT_DESC, $rank);

echo "<ol>";
for ($line = 0; $line < 6; $line++){
    echo "<li><b> Rank</b>";
    foreach($rank[$line] as $key => $value){
        echo "|".$key."-".$value."|";
    }
    echo "</li>";
}    
echo "</ol>";
?>