我想知道如何在PHP中基于" leaguePoints "然后在" 获胜"
<?php
foreach ($league as $leaguesList)
{
if ($leaguesList->queue == "RANKED_SOLO_5x5")
{
foreach ($leaguesList as $list)
{
if ($list->division == $leagueSummoner->division){
$array[] = $list;
}
}
}
}
var_dump($array);
结果: http://pastebin.com/QkKU7W0k
我试图用Java来解决这个问题,我是这样做的:
public void setRankList(List<LeagueSummonerLeagueStats> _rankList) {
rankList = _rankList;
Collections.sort(rankList, new Comparator() {
public int compare(Object match1Obj, Object match2Obj) {
LeagueSummonerLeagueStats league1 = (LeagueSummonerLeagueStats) match1Obj;
LeagueSummonerLeagueStats league2 = (LeagueSummonerLeagueStats) match2Obj;
int i = Integer.compare(league2.getLeaguePoints(), league1.getLeaguePoints());
if (i != 0)
return i;
i = Integer.compare(league2.getWins(), league1.getWins());
if (i != 0)
return i;
return -1;
}
});
}
非常感谢您的时间,非常感谢您的帮助。
答案 0 :(得分:0)
你会想要usort()
这样使用:
function myCompare($a, $b) {
// comparison code here
}
usort($mydata, 'myCompare');
或使用匿名函数:
usort($mydata, function($a, $b){ /* ... */ });
或者您可以输入任何接受两个参数的Callable并返回负数,零或正数。
答案 1 :(得分:0)
我不想通常提供解决方案,但您只需要将其移植到另一种语言。
基本上,你想写一个bubblesort回调,它首先会比较两个记录的胜利属性,然后如果它们相等,则回到联盟积分属性。像
这样的东西$data = [
[
'wins' => 10,
'leaguePoints' => 10,
],
[
'wins' => 10,
'leaguePoints' => 20,
],
[
'wins' => 20,
'leaguePoints' => 10,
],
[
'wins' => 10,
'leaguePoints' => 10,
],
];
usort($data, function($a, $b)
{
if ($a['wins'] === $b['wins']) {
if ($a['leaguePoints'] === $b['leaguePoints']) {
return 0;
}
return ($a['leaguePoints'] > $b['leaguePoints']) ? -1 : 1;
}
return ($a['wins'] > $b['wins']) ? -1 : 1;
});
print_r($data);
答案 2 :(得分:0)
不确定我的索引是否正确。
foreach($league as $leaguesList){
foreach ($leaguesList as $key => $list){
$leaguePoints = [$key] ["info"]["leaguePoints"];
$wins = [$key] ["info"]["info"]["wins"];
$sort = $leaguePoints *= 100 + $wins;
$sorted[$sort] = $list;
}
}
ksort($sorted);