2

I have an array and i want to sort it by value of key [min] (0.2, 0.86 ...) in a desc order. Here is an array:

Array
(
    [0] => Array
        (
            [p1_first_res_avalue] => 0.72413793103448
            [p1_rating_lang_avalue] => 0.2
            [p1_ps_res_avalue] => 0.79310344827586      
            [pid] => 0
            [p1_discipline_e_avalue] => 0.77777777777778
            [p1_rating_lang] => 46
            [p1_first_res] => 59
            [p1_ps_res] => 57
            [p1_discipline_e] => 86
            [min] => 0.2
        )

    [1] => Array
        (
            [p1_discipline_e] => 81
            [p1_first_res] => 55
            [p1_rating_lang] => 38
            [p1_ps_res] => 48
            [p1_discipline_e_avalue] => 1
            [pid] => 1
            [p1_first_res_avalue] => 0.86206896551724
            [p1_rating_lang_avalue] => 1
            [p1_ps_res_avalue] => 1
            [min] => 0.86
        )

   [2] => Array
        (
            [p1_discipline_e] => 81
            [p1_first_res] => 55
            [p1_rating_lang] => 38
            [p1_ps_res] => 48
            [p1_discipline_e_avalue] => 1
            [pid] => 1
            [p1_first_res_avalue] => 0.86206896551724
            [p1_rating_lang_avalue] => 1
            [p1_ps_res_avalue] => 1
            [min] => 0.3
        )
...
)

I've tried to use uasort function, but I can't get access to [min] value of array to compare it. That's what i try ($res is an array, need to sort):

$sortd = 'down';
$f = function($a, $b) use ($sortd) {
        if (($sortd) == 'down') {
            if ($a['min'] == $b['min']) return 0;
            return ($a['min'] > $b['min']) ? -1 : 1;
        }
        else {

        }
    };

foreach ($res as $k => $v) {
    uasort($res[$k], $f);
}

Please, any ideas how to solve the problem?

1

4 Answers 4

2

Your problem seems to be that you're trying to apply the sorting function to each element of the array individually, instead of the array as a whole.

Instead of

foreach ($res as $k => $v) {
    uasort($res[$k], $f);
}

Try just

uasort($res, $f);

Sign up to request clarification or add additional context in comments.

Comments

1

Well... I bet nobody expects the Spanish Inquisition that's there is a built-in solution which is about 25 times faster. Please, forget about sorting 2d arrays with u*sort, it worths nothing to just read the manual!

http://php.net/manual/en/function.array-multisort.php

For given case, it's just:

array_multisort(array_column($res, 'min'), SORT_DESC, SORT_NUMERIC, $res);

Even more, it can sort by several columns at once, much like ORDER BY, e.g.:

// ORDER BY `name` ASC, `price` DESC:
array_multisort(
    array_column($rows, 'name'), SORT_ASC,
    array_column($rows, 'price'), SORT_DESC, SORT_NUMERIC,
    $rows
);

Two things you should be aware of: array_column skips rows without requested key, but this is not a problem for data fetched from SELECT or something like that (note that null values are not omitted) and array_multisort doesn't preserve numeric keys. However, both things can be easily worked around without any significant overhead.

Simple benchmark for curious: https://gist.github.com/offshore/fb836c1204da85fe2f34557d970350f9

Comments

0

Why wouldn't uasort work? Your comparator would just be

$f = function($a1, $a2) { return $a1['min'] < $a2['min'] ? -1 : 1; };
uasort($res, $f);

You're sorting each subarray - there's no min in thoses values, so there would all be 0. Your function returns 0 when that is the case, so the sort would do nothing.

Comments

0

uasort may incur significant overhead.

If you want to sort an array of arrays, you can create a flattened version and use asort.

$flat = array();
foreach ($arr as $index => $item) {
    $flat[$index] = $item[$k];
}
asort($flat);

If you want to check the performance, you can use microtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.