Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple sorts in an array

I have the following array:

Array
(
[0] => Array
    (
        [note] => test
        [year] => 2011
        [type] => football
    )

[1] => Array
    (
        [note] => test1
        [year] => 2010
        [type] => basket
    )

[2] => Array
    (
        [note] => test2
        [year] => 2012
        [type] => football
    )

[3] => Array
    (
        [note] => test3
        [year] => 2009
        [type] => basket
    )

[4] => Array
    (
        [note] => test4
        [year] => 2010
        [type] => football
    )

)

And I would like to first sort it according another array by type:

For example: $sort = array('football','basket');

And afterwards by year.

How can I do that?

Thanks.

Desired output should be:

Array
(
[2] => Array
    (
        [note] => test2
        [year] => 2012
        [type] => football
    )
[0] => Array
    (
        [note] => test
        [year] => 2011
        [type] => football
    )
[4] => Array
    (
        [note] => test4
        [year] => 2010
        [type] => football
    )

[1] => Array
    (
        [note] => test1
        [year] => 2010
        [type] => basket
    )

[3] => Array
    (
        [note] => test3
        [year] => 2009
        [type] => basket
    )

)

I do not mind if we reset the index values.

Thanks.

like image 641
glarkou Avatar asked Jul 29 '12 08:07

glarkou


People also ask

How do I sort multiple arrays?

The arrays are treated as columns of a table to be sorted by rows. The first array is the main one to sort by; all the items in the other arrays are reordered based on the sorted order of the first array. If items in the first array compare as equal, the sort order is determined by the second array, and so on.

Which function sorts multiple array at once?

array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative (string) keys will be maintained, but numeric keys will be re-indexed.

How do you sort an array with two conditions?

isImportant){ return 1; }else{ return b. date-a. date; } }); The sort() callback function usually receives two arguments, say a and b, which are nothing but two elements of the array on which sort() was called and the callback function runs for each possible pair of elements of the array.

What is multi sort?

The function MULTISORT performs a sorting operation with multiple. sort keys. Unlike the IDL built-in SORT() function, which can. only sort a single key, MULTISORT can accept multiple keys.


1 Answers

Requires a version of PHP that supports closures when run as-is

Your secondary array can be defined outside of the sort itself. If you can't support closures, then this can be put directly into the sort routine.

$sortBy = array('football', 'basket');

Sort routine.

Sorts by year if types are the same, and sorts by the types in your $sortBy array otherwise.

usort($a, function($a, $b) use ($sortBy) {
    if ($a['type'] == $b['type']) {
        return ($b['year'] - $a['year']);
    }
    $typeOrder = array_flip($sortBy);
    return $typeOrder[$a['type']] - $typeOrder[$b['type']];
});
like image 106
Leigh Avatar answered Oct 03 '22 08:10

Leigh