Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel's array_sort helper DESC e ASC

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.

like image 208
almo Avatar asked Jul 07 '14 14:07

almo


People also ask

How to sort an array by value in laravel?

The sort helper method will allow you to sort the given $array based on some condition returned by the $callback . The method works by iterating over all the values in the $array and passing the values it finds into the $callback function.

How to sort an array of objects in laravel?

public function sortByScore ($a, $b) { return strcmp($a->score, $b->score); } $mySortedData = usort($myData, array($this, 'sortByScore'));

How to sort items in laravel?

To sort results in the database query, you'll need to use the orderBy() method, and provide the table field you want to use as criteria for ordering. This will give you more flexibility to build a query that will obtain only the results you need from the database.

What is Ksort PHP?

The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value.


2 Answers

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.

like image 120
Aken Roberts Avatar answered Oct 16 '22 08:10

Aken Roberts


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]);
}));
like image 24
maxwilms Avatar answered Oct 16 '22 08:10

maxwilms