Laravel Collection SortByDesc Tutorial with Examples
Hi Artisan,
In this tutorial, i will show you laravel collection sort by desc. I’m going to show you about laravel eloquent sortbydesc. you can understand a concept of sortbydesc laravel example. We will use laravel collection sort by name.
I will give you list of examples of sort by desc colletion in laravel. so you can easily use it with your laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application. so let's see bellow example that will helps you lot.
1) Example 1: Laravel Collection Sort By Desc Simple Example
2) Example 2: Laravel Collection Sort By Desc Count
3) Example 3: Laravel Collection Sort By Date Desc
4) Example 4: Laravel Collection Sort By Desc Multiple Coulmns
5) Example 5: Laravel Collection Sort By Desc Relation
Example 1: Laravel Collection Sort By Desc Simple Example
public function index()
{
$collection = collect([
['id' => 1, 'name' => 'Hardik', 'email' => 'hardik@gmail.com'],
['id' => 2, 'name' => 'Ankit', 'email' => 'ankit@gmail.com'],
['id' => 3, 'name' => 'Balo', 'email' => 'balo@gmail.com'],
]);
$sorted = $collection->sortByDesc('name');
$sorted->all();
dd($sorted);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[id] => 1
[name] => Hardik
[email] => hardik@gmail.com
)
[2] => Array
(
[id] => 3
[name] => Balo
[email] => balo@gmail.com
)
[1] => Array
(
[id] => 2
[name] => Ankit
[email] => ankit@gmail.com
)
)
)
Example 2: Laravel Collection Sort By Desc 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);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[2] => Array
(
[id] => 3
[name] => Apple
[models] => Array
(
[0] => s5
[1] => s6
[2] => s7
)
)
[0] => Array
(
[id] => 1
[name] => Vivo
[models] => Array
(
[0] => v11
[1] => v85
)
)
[1] => Array
(
[id] => 2
[name] => Appo
[models] => Array
(
[0] => a23
)
)
)
)
Example 3: Laravel Collection Sort By Date Desc
public function index()
{
$collection = collect([
['id' => 1, 'name' => 'Hardik', 'created_date' => '2020-04-05'],
['id' => 2, 'name' => 'Ankit', 'created_date' => '2020-04-01'],
['id' => 3, 'name' => 'Balo', 'created_date' => '2020-04-03'],
]);
$sorted = $collection->sortByDesc('created_date');
$sorted->all();
dd($sorted);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[id] => 1
[name] => Hardik
[created_date] => 2019-04-05
)
[2] => Array
(
[id] => 3
[name] => Balo
[created_date] => 2020-04-03
)
[1] => Array
(
[id] => 2
[name] => Ankit
[created_date] => 2020-04-01
)
)
)
Example 4: Laravel Collection Sort By Desc Multiple Coulmns
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);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[2] => Array
(
[id] => 3
[name] => Balo
[city] => Rajkot
)
[1] => Array
(
[id] => 2
[name] => Ankit
[city] => Rajkot
)
[0] => Array
(
[id] => 1
[name] => Hardik
[city] => Mumbai
)
[3] => Array
(
[id] => 4
[name] => Ankit
[city] => Mumbai
)
)
)
Example 5: Laravel Collection Sort By Desc Relation
public function index()
{
$posts = Post::get()->sortByDesc(function($query){
return $query->auther->name;
})
->all();
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- Laravel Collection Unique | Remove Duplicates from Collection Laravel
- Laravel Collection Search Method Example
- Laravel Collection Filter Method Example
- Laravel 8/7 Paginate with Collection or Array
- Merge Multiple Collection Paginate in Laravel
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel Many to Many Polymorphic Relationship Tutorial