Laravel Sortby Descending (sortByDesc) Collection Example

laravel sortby desc order exampe: In this tutorial you will learn how to use sort by date, name, id desc and sortby asc or desc in laravel application. The below examples will help you laravel eloquent order by desc.

This tutorial will explain you easy way laravel collection sort by desc two fields to the laravel collection or object sort by desc.

Example 1: Laravel sortByDesc Simple Example

Here we are using sortByDesc method to sort the collection using the name field;

public function index()
{
    $collection = collect([
        ['id' => 1, 'name' => 'Rohit', 'email' => 'rohit@example.com'],
        ['id' => 2, 'name' => 'Ashish', 'email' => 'ashish@example.com'],
        ['id' => 3, 'name' => 'Mohit', 'email' => 'mohit@example.com']
    ]);

    $sorted = $collection->sortByDesc('name');
    dd($sorted->all());
}

Output:

array:3 [▼
  0 => array:3 [▼
    "id" => 1
    "name" => "Rohit"
    "email" => "rohit@example.com"
  ]
  2 => array:3 [▼
    "id" => 3
    "name" => "Mohit"
    "email" => "mohit@example.com"
  ]
  1 => array:3 [▼
    "id" => 2
    "name" => "Ashish"
    "email" => "ashish@example.com"
  ]
]

Example 2: Laravel Collection Sort By Date Desc

Here we are sorting the collection using the created_date field

public function index()
{
    $collection = collect([
        ['id' => 1, 'name' => 'Rohit', 'email' => 'rohit@example.com', 'created_date' => '2020-09-05'],
        ['id' => 2, 'name' => 'Ashish', 'email' => 'ashish@example.com', 'created_date' => '2019-07-03'],
        ['id' => 3, 'name' => 'Mohit', 'email' => 'mohit@example.com', 'created_date' => '2015-04-04']
    ]);

    $sorted = $collection->sortByDesc('created_date');
    dd($sorted->all());
}

Output:

array:3 [▼
  0 => array:4 [▼
    "id" => 1
    "name" => "Rohit"
    "email" => "rohit@example.com"
    "created_date" => "2020-09-05"
  ]
  1 => array:4 [▼
    "id" => 2
    "name" => "Ashish"
    "email" => "ashish@example.com"
    "created_date" => "2019-07-03"
  ]
  2 => array:4 [▼
    "id" => 3
    "name" => "Mohit"
    "email" => "mohit@example.com"
    "created_date" => "2015-04-04"
  ]
]

Example 3: Laravel Collection Sort By Desc Count

This example sorting data with count;

public function index()
{
    $collection = collect([
            ['id' => 1, 'name' => 'Vivo', 'models' => ['v11', 'v85']],
            ['id' => 2, 'name' => 'Appo', 'models' => ['a23']],
            ['id' => 3, 'name' => 'Apple', 'models' => ['s5', 's6', 's7']],
        ]);

    $sorted = $collection->sortByDesc(function ($product, $key) {
                        return count($product['models']);
                    });

    $sorted->all();
    dd($sorted);
}

Example 4: Sort By Desc Multiple Coulmns

We can sort the data descending order just like below:

public function index()
{
    $collection = collect([
            ['id' => 1, 'name' => 'Hardik', 'city' => 'Mumbai'],
            ['id' => 2, 'name' => 'Ankit', 'city' => 'Rajkot'],
            ['id' => 3, 'name' => 'Balo', 'city' => 'Rajkot'],
            ['id' => 4, 'name' => 'Ankit', 'city' => 'Mumbai'],
        ]);

    $sorted = $collection->sortByDesc(function ($product, $key) {
                        return $product['city'].$product['name'];
                    });

    $sorted->all();
    dd($sorted);
}

Hope these examples help you..

Leave a Comment