自定义按最高值对n个项目进行多维排序 [英] Custom sorting multidimensional with n items by highest value

查看:62
本文介绍了自定义按最高值对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);

这篇关于自定义按最高值对n个项目进行多维排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆