按多个字段对数组进行排序
Sorting an array by multiple fields
在我的后端,我有一些代码处理团队用于循环赛小组赛,最后团队以数组形式返回并包含比赛 results/stats 等
我有以下数组,我想按以下顺序按两个字段排序:points
(desc), round_difference
(desc).
最终的正确顺序是:TeamOne
、TeamThree
、TeamTwo
。
我将如何处理这种格式的数组?有可能吗?我查看了 array_multisort()
但是我的数组 keys/structure 总体上有点 "odd" 与我在页面上找到的其他代码示例相比我很困惑。
希望有人能帮助我。
Array
(
[1] => Array
(
[1] => Array
(
[id] => 1
[group] => 1
[name] => TeamOne
[tag] => One
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 1
[ot_lost] => 0
[lost] => 0
[round_difference] => 3
[points] => 2
)
)
[238] => Array
(
[id] => 238
[group] => 1
[name] => TeamTwo
[tag] => Two
[matches] => Array
(
[played] => 0
[won] => 0
[ot_won] => 0
[ot_lost] => 0
[lost] => 0
[round_difference] => 0
[points] => 0
)
)
[14] => Array
(
[id] => 14
[group] => 1
[name] => TeamThree
[tag] => Three
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 1
)
)
)
尝试 uasort()
并像这样比较键:
uasort($arr[1],function($a,$b){
# If the points are more/less right off the bat, return results
if($a['matches']['points'] < $b['matches']['points'])
return true;
# If they are the same
elseif($a['matches']['points'] == $b['matches']['points'])
# Compare round_difference sort by that
return ($a['matches']['round_difference'] < $b['matches']['round_difference']);
});
因此,排序如下:
$arr = array(
1 => array(
1 => array(
'id' => 1,
'group' => 1,
'name' => 'TeamOne',
'tag' => 'One',
'matches' => array(
'played' => 1,
'won' => 0,
'ot_won' => 1,
'ot_lost' => 0,
'lost' => 0,
'round_difference' => 3,
'points' => 2
)
),
238 => array
(
'id' => 238,
'group' => 1,
'name' => 'TeamTwo',
'tag' => 'Two',
'matches' => array(
'played' => 0,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 0,
'lost' => 0,
'round_difference' => 0,
'points' => 0,
)
),
14 => array
(
'id' => 14,
'group' => 1,
'name' => 'TeamThree',
'tag' => 'Three',
'matches' => array
(
'played' => 1,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 1,
'lost' => 0,
'round_difference' => '-3',
'points' => 1,
)
),
17 => array
(
'id' => 17,
'group' => 1,
'name' => 'TeamFive',
'tag' => 'Five',
'matches' => array
(
'played' => 1,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 1,
'lost' => 0,
'round_difference' => '-3',
'points' => 2,
)
)
)
);
给你:
Array
(
[1] => Array
(
[1] => Array
(
[id] => 1
[group] => 1
[name] => TeamOne
[tag] => One
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 1
[ot_lost] => 0
[lost] => 0
[round_difference] => 3
[points] => 2
)
)
[17] => Array
(
[id] => 17
[group] => 1
[name] => TeamFive
[tag] => Five
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 2
)
)
[14] => Array
(
[id] => 14
[group] => 1
[name] => TeamThree
[tag] => Three
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 1
)
)
[238] => Array
(
[id] => 238
[group] => 1
[name] => TeamTwo
[tag] => Two
[matches] => Array
(
[played] => 0
[won] => 0
[ot_won] => 0
[ot_lost] => 0
[lost] => 0
[round_difference] => 0
[points] => 0
)
)
)
)
在我的后端,我有一些代码处理团队用于循环赛小组赛,最后团队以数组形式返回并包含比赛 results/stats 等
我有以下数组,我想按以下顺序按两个字段排序:points
(desc), round_difference
(desc).
最终的正确顺序是:TeamOne
、TeamThree
、TeamTwo
。
我将如何处理这种格式的数组?有可能吗?我查看了 array_multisort()
但是我的数组 keys/structure 总体上有点 "odd" 与我在页面上找到的其他代码示例相比我很困惑。
希望有人能帮助我。
Array
(
[1] => Array
(
[1] => Array
(
[id] => 1
[group] => 1
[name] => TeamOne
[tag] => One
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 1
[ot_lost] => 0
[lost] => 0
[round_difference] => 3
[points] => 2
)
)
[238] => Array
(
[id] => 238
[group] => 1
[name] => TeamTwo
[tag] => Two
[matches] => Array
(
[played] => 0
[won] => 0
[ot_won] => 0
[ot_lost] => 0
[lost] => 0
[round_difference] => 0
[points] => 0
)
)
[14] => Array
(
[id] => 14
[group] => 1
[name] => TeamThree
[tag] => Three
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 1
)
)
)
尝试 uasort()
并像这样比较键:
uasort($arr[1],function($a,$b){
# If the points are more/less right off the bat, return results
if($a['matches']['points'] < $b['matches']['points'])
return true;
# If they are the same
elseif($a['matches']['points'] == $b['matches']['points'])
# Compare round_difference sort by that
return ($a['matches']['round_difference'] < $b['matches']['round_difference']);
});
因此,排序如下:
$arr = array(
1 => array(
1 => array(
'id' => 1,
'group' => 1,
'name' => 'TeamOne',
'tag' => 'One',
'matches' => array(
'played' => 1,
'won' => 0,
'ot_won' => 1,
'ot_lost' => 0,
'lost' => 0,
'round_difference' => 3,
'points' => 2
)
),
238 => array
(
'id' => 238,
'group' => 1,
'name' => 'TeamTwo',
'tag' => 'Two',
'matches' => array(
'played' => 0,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 0,
'lost' => 0,
'round_difference' => 0,
'points' => 0,
)
),
14 => array
(
'id' => 14,
'group' => 1,
'name' => 'TeamThree',
'tag' => 'Three',
'matches' => array
(
'played' => 1,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 1,
'lost' => 0,
'round_difference' => '-3',
'points' => 1,
)
),
17 => array
(
'id' => 17,
'group' => 1,
'name' => 'TeamFive',
'tag' => 'Five',
'matches' => array
(
'played' => 1,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 1,
'lost' => 0,
'round_difference' => '-3',
'points' => 2,
)
)
)
);
给你:
Array
(
[1] => Array
(
[1] => Array
(
[id] => 1
[group] => 1
[name] => TeamOne
[tag] => One
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 1
[ot_lost] => 0
[lost] => 0
[round_difference] => 3
[points] => 2
)
)
[17] => Array
(
[id] => 17
[group] => 1
[name] => TeamFive
[tag] => Five
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 2
)
)
[14] => Array
(
[id] => 14
[group] => 1
[name] => TeamThree
[tag] => Three
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 1
)
)
[238] => Array
(
[id] => 238
[group] => 1
[name] => TeamTwo
[tag] => Two
[matches] => Array
(
[played] => 0
[won] => 0
[ot_won] => 0
[ot_lost] => 0
[lost] => 0
[round_difference] => 0
[points] => 0
)
)
)
)