Laravel's array_sort helper DESC e ASC

10,195

Solution 1

The array_sort() helper function is a very thin wrapper around the default Illuminate\Support\Collection::sortBy() method. Excluding comments, this is all that it does:

function array_sort($array, Closure $callback)
{
    return \Illuminate\Support\Collection::make($array)->sortBy($callback)->all();
}

While handy, it is limiting in its sorting capabilities. Similarly, the Collection class will allow you to change sort direction, but not much else.

In my opinion, you have two options:

  1. Skip a Laravel-only solution and use some normal PHP and array_multisort(). As @Jon commented, there's some great details in this SO question.

  2. Use a combination of grouping and sorting in a Collection object to achieve the results you want.

I'd just stick with #1.

Solution 2

Just as an example how to sort by first name and then by last name with the sort helper of Laravel.

First define some more example data:

$array = array(
    array('firstname_1','lastname_1'),
    array('firstname_2','lastname_3'),
    array('firstname_2','lastname_2'),
    array('firstname_3','lastname_3'),
);

In our closure we want to sort by first name first, and then by last name. Laravel will sort by the returned values of the closure. So in your case the trick is to concatenate both strings:

$array = array_sort($array, function($value) {
    return sprintf('%s,%s', $value[0], $value[1]);
});

Internally Laravel will now sort the contents of this intermediate array:

$intermediateArray = array(
    'firstname_1,lastname_1',
    'firstname_2,lastname_3',
    'firstname_2,lastname_2',
    'firstname_3,lastname_3',
);

This will result in an array which is sorted by first name and than by last name in ascending order.

To use descending order you need first sort the array and than reverse it:

$array = array_reverse(array_sort($array, function($value) {
    return sprintf('%s,%s', $value[0], $value[1]);
}));
Share:
10,195
almo
Author by

almo

PHP developer

Updated on June 27, 2022

Comments

  • almo
    almo almost 2 years

    I want to sort a multidimensionl array by one or more keys using the Laravel helper array_sort.

    array(
        array('firstname_1','lastname_1'),
        array('firstname_2','lastnmae_2')
    )
    

    I want to order it first by firstname and then by lastname.

    I also want to do this in DESC or ASC order. How can I achieve this?

    There are functions aivalable in the internet to do this but I would like to understand how to use the Laravel helper. The doc for array_sort (http://laravel.com/docs/helpers#arrays) I don't find comprehensive.

  • dustbuster
    dustbuster over 4 years
    I am updating a site from laravel 4.2 to 6.5 and this was really helpful! I added the forward slash in front of the "Illuminate" because I got an error initially. This code is tested in a laravel environment.