Friday, 19 April 2019

Custom sorting multidimensional with n items by highest value

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?

Test Array:

$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",
        ]],
    ],
]];

Sorting Function

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]);

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
                        )

                )

        )

)

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.



from Custom sorting multidimensional with n items by highest value

No comments:

Post a Comment