Laravel

Laravel Collection Sort By Value With Example

Hello buddy, in this article, we will learn about how to sort data in laravel collection, I will show you the easiest way to sort the laravel collection.

I will give you a list of examples of sort by collection in laravel. so you can easily use it with your laravel 5, laravel 6, laravel 7 and laravel 8 application. so let’s see the below example that will help you a lot.

We will see below laravel sorting collection example.

  1. Sort Example
  2. Sort By
  3. SortByDesc
  4. SortKeys
  5. SortKeysDesc

Sort Example

The sort method sorts the collection. The sorted collection keeps the original array keys, so in the following example we will use the values method to reset the keys to consecutively numbered indexes:

Look for the below example for a better understanding.

$collection = collect([5, 3, 1, 2, 4]);

$sorted = $collection->sort();

$sorted->values()->all();

// [1, 2, 3, 4, 5]

Sort By Example

The sortBy method sorts the collection by the given key. The sorted collection keeps the original array keys, so in the following example we will use the values method to reset the keys to consecutively numbered indexes:

See the below example.

$collection = collect([
    ['name' => 'Desk', 'price' => 200],
    ['name' => 'Chair', 'price' => 100],
    ['name' => 'Bookcase', 'price' => 150],
]);

$sorted = $collection->sortBy('price');

$sorted->values()->all();

/*
    [
        ['name' => 'Chair', 'price' => 100],
        ['name' => 'Bookcase', 'price' => 150],
        ['name' => 'Desk', 'price' => 200],
    ]
*/

SortBy Desc Example

This method will sort the collection in the opposite order as the sort method, as per the name it will sort the data in descending order.

See the below example to understand it in a better way.



$collection = collect([
    ['name' => 'Desk', 'price' => 200],
    ['name' => 'Chair', 'price' => 100],
    ['name' => 'Bookcase', 'price' => 150],
]);

$sorted = $collection->sortByDesc('price');

$sorted->values()->all();

/*
    [
      ['name' => 'Desk', 'price' => 200],
      ['name' => 'Chair', 'price' => 100],
      ['name' => 'Bookcase', 'price' => 150],
    ]
*/

SortKeys Example

The sortKeys method sorts the collection by the keys of the underlying associative array:

See the below example.

$collection = collect([
    'id' => 22345,
    'first' => 'John',
    'last' => 'Doe',
]);

$sorted = $collection->sortKeys();

$sorted->all();

/*
    [
        'first' => 'John',
        'id' => 22345,
        'last' => 'Doe',
    ]
*/

SortKeysDesc Example

This method has the same signature as the sortKeys method but will sort the collection in the opposite order. As the name suggests it sorts the array key in descending order.

Look for the below example so that you have a fair idea about SortKeysDesc



$collection = collect([5, 3, 1, 2, 4]);

$sorted = $collection->sort();

$sorted->values()->all();

// [5, 4, 3, 2, 1]

Now I think that you have understood that how we can use laravel sort by value, or what are ways in which we can sort laravel collection, if you want to get deeper knowledge about the laravel collection you can refer to the official website.

If you still have confusion feel free to put a comment, will try to reply as soon as possible.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button