自定义按最高值对n个项目进行多维排序
我目前能够使用自定义排序方法对多维数组进行排序.每个数组lineupSet
都有n
个项目.函数sort_points
将对每个lineupSet
从最高totalPoints
到最低的totalPoints
进行排序,然后将给我lineupSet
总的totalPoints
最高.我当前正在改变方法,我仍然想首先对每个lineupSet
进行排序,并按从高到低的顺序排序.然后,我想根据给定的计数获得每个lineupSet
中的最高totalPoints
.解决这个问题的最佳方法是什么?
I am currently able to sort a multidimensional array using a custom sorting method. Each array lineupSet
has an n
amount of items. The function sort_points
will sort each lineupSet
from highest to lowest totalPoints
and then it will give me the lineupSet
with the the highest total totalPoints
. I am currently changing the approach, I still want to sort through each lineupSet
first and order highest to lowest. Then I would like to get the highest totalPoints
of each lineupSet
based on a given count. What would be the best way to approach this?
测试数组:
$testArray = [[
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 214.61,
],
"name" => "arr0-test0",
], [
"formula" => [
"totalPoints" => 201.17,
],
"name" => "arr0-test1",
]], [
"formula" => [
"totalPoints" => 5.01,
],
"name" => "arr0-test2",
]],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 214.76,
],
"name" => "arr1-test0",
], [
"formula" => [
"totalPoints" => 220.66,
],
"name" => "arr1-test1",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.71,
],
"name" => "arr2-test0",
], [
"formula" => [
"totalPoints" => 204.43,
],
"name" => "arr2-test1",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.48,
],
"name" => "arr3-test0",
], [
"formula" => [
"totalPoints" => 203.51,
],
"name" => "arr3-test1",
]],
],
]];
排序功能
function sum_points($v) {
$totalPoints = 0;
foreach ($v['lineupSet'] as $lset) {
if (isset($lset['formula'])) {
$totalPoints += $lset['formula']['totalPoints'];
}
else {
foreach ($lset as $l) {
$totalPoints += $l['formula']['totalPoints'];
}
}
}
return $totalPoints;
}
function sort_points($a, $b) {
return sum_points($b) - sum_points($a);
}
usort($testArray, 'sort_points');
print_r($testArray[0]);
例如,我想获得最高的两个最高"totalPoints".理想的结果:
For example I want to get the top two highest 'totalPoints'. The desired outcome:
Array (
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)
[name] => arr1-test1
)
[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)
[name] => arr0-test0
)
)
)
)
我想对最高的n
最高的totalPoints
做同样的事情.请记住,有时每个lineupSet
中的n
个项目都必须是最高的totalPoints
.
I want to do the same for the top n
highest totalPoints
. Keeping in mind that it will have to take at times n
items from each lineupSet
that are the highest totalPoints
.
我认为最好使用一个对象,然后在对数据进行排序时可以保留max(也可以使用构造函数对数组进行排序).>
I think it's better to use an object then you can keep max while you are sorting data (also you can use a constructor to sort the array).
Class SortHelper{
public $max = 0;
private function checkMax($totalPoints){
if($totalPoints > $this->max)
$this->max = $totalPoints;
}
private function sum_points($v) {
$totalPoints = 0;
foreach ($v['lineupSet'] as $lset) {
if (isset($lset['formula'])) {
$totalPoints += $lset['formula']['totalPoints'];
$this->checkMax($lset['formula']['totalPoints']);
}
else {
foreach ($lset as $l) {
$totalPoints += $l['formula']['totalPoints'];
$this->checkMax($l['formula']['totalPoints']);
}
}
}
return $totalPoints;
}
private function sort_points($a, $b) {
return $this->sum_points($b) - $this->sum_points($a);
}
public function sort($array){
usort( $array, [$this, 'sort_points']);
return $array;
}
}
那么您将拥有:
$sortHelper = new SortHelper();
$sorted_array = $sortHelper->sort($testArray);
var_dump($sorted_array[0]);
var_dump($sortHelper->max);