Laravel collection sortByDesc example

In this tutorial, I will show you how to use Laravel collection sortByDesc. Sorting data in Laravel collections is a common requirement, especially when dealing with lists where you need to organize data in descending order.

I will provide you with several examples of sortByDesc collection usage in Laravel. This will help you easily implement it in your tasks.

What is sortByDesc()?

The sortByDesc() method in Laravel is used to sort a collection by a specified key or callback in descending order. It works seamlessly with both simple and complex datasets.

  1. Example 1: Simple Example of Laravel Collection SortByDesc
  2. Example 2: Sorting a Collection by a Numeric Key
  3. Example 3: Sorting a Collection by Count
  4. Example 4: Sorting a Collection by a String Key
  5. Example 5: Laravel Collection SortByDesc with Relationships
Example 1: Simple Example of Laravel Collection SortByDesc
public function index()
    {
        $collection = collect([
            ['id' => 1, 'title' => 'This is first title', 'description' => 'This is best solution'],
            ['id' => 2, 'title' => 'This is Second title', 'description' => 'Best description'],        
            ['id' => 3, 'title' => 'This is Third title', 'description' => 'Good description'],
        ]);

        $sortedData = $collection->sortByDesc('description');

        $sortedData->all();

        dd($sortedData);
    }
Output:
 0 => array:3 [▼
      "id" => 1
      "title" => "This is first title"
      "description" => "This is best solution"
    ]
 2 => array:3 [▼
      "id" => 3
      "title" => "This is Third title"
      "description" => "Good description"
    ]
 1 => array:3 [▼
      "id" => 2
      "title" => "This is Second title"
      "description" => "Best description"
    ]
Example 2: Sorting a Collection by a Numeric Key

You have a collection of data with available attribute, and you want to sort them in descending order of available.

 public function index()
    {
        $data = collect([
            ['type' =>  'A', 'available' => 100],
            ['type' =>  'B', 'available' => 150],
            ['type' =>  'C', 'available' => 175],
        ]);

        $sortedData = $data->sortByDesc('available');
        dd($sortedData->values()->all());
    }
Example 3: Sorting a Collection by Count
 public function index()
    {
        $collection = collect([
            ['id' => 1, 'role' => ['Super Admin', 'Admin']],
            ['id' => 2, 'role' => ['user']],
            ['id' => 3, 'role' => ['Super Admin', 'Admin', 'Manager']],
        ]);

        $sorteData = $collection->sortByDesc(function ($data, $key) {
            return count($data['role']);
        });

        dd($sorteData->all());
    }
Example 4: Sorting a Collection by a String Key

Consider a list of data with title attributes that need to be sorted in descending order.

 public function index()
    {
        $collection = collect([
           ['title' => 'First title'],
           ['title' => 'Second title'], 
           ['title' => 'Third title'],
        ]);

       $sorteData = $collection->sortByDesc('title');

        dd($sorteData->all());
    }
Example 5: Laravel Collection SortByDesc with Relationships
 public function index()
    {
        $users = User::get()->sortByDesc(function ($query) {
            return $query->role->name;
        })->all();
    }

I hope this tutorial help you.

Leave a Reply

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