自定义按最高值对n个项目进行多维排序

Mar*_*ing 8 php arrays sorting

我目前能够使用自定义排序方法对多维数组进行排序。每个数组lineupSet都有n一定数量的项目。该函数sort_points将按lineupSet从高到低的顺序对每个对象进行排序totalPoints,然后将给我lineupSet总计最高的totalPoints。我目前正在改变方法,我仍然想按lineupSet优先顺序从高到低排序。然后,我想根据给定的数量获得最高totalPointslineupSet收益。解决此问题的最佳方法是什么?

测试数组:

$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",
        ]],
    ],
]];
Run Code Online (Sandbox Code Playgroud)

分类功能

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]);
Run Code Online (Sandbox Code Playgroud)

例如,我想获得最高的两个最高“ totalPoints”。预期的结果:

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
                        )

                )

        )

)
Run Code Online (Sandbox Code Playgroud)

我也想为n最高排名做同样的事情totalPoints。请记住,有时必须n从每个项目中拿走lineupSet最高的项目totalPoints

Sae*_*.Gh 5

我认为最好使用一个对象,然后可以在对数据进行排序时保留max(也可以使用构造函数对数组进行排序)。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么您将拥有:

$sortHelper = new SortHelper();
$sorted_array = $sortHelper->sort($testArray);

var_dump($sorted_array[0]);
var_dump($sortHelper->max);
Run Code Online (Sandbox Code Playgroud)