How to sortByDesc Collection in Laravel 12

In this tutorial, I’ll show you how to sort collections in Laravel 12 using sortByDesc() and other helpful methods. Whether you're sorting by simple values, counts, dates, multiple columns, or even relationship data, I’ll walk you through each example step by step with simple and clear code snippets.

How to sortByDesc a Collection in Laravel 12

Laravel Collections provide powerful methods like sortBy(), sortByDesc(), and sort() to manipulate arrays and data structures easily. Below are several ways to sort your collection in descending order.

How to Sort Collection in Laravel 12

 

Example 1: Sort By Desc

use Illuminate\Support\Collection;

$collection = collect([
    ['name' => 'John', 'score' => 75],
    ['name' => 'Alice', 'score' => 90],
    ['name' => 'Bob', 'score' => 85],
]);

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

$sorted->values()->all();
// Output:
// [
//     ['name' => 'Alice', 'score' => 90],
//     ['name' => 'Bob', 'score' => 85],
//     ['name' => 'John', 'score' => 75],
// ]

 

Example 2: Sort By Desc Count

$collection = collect([
    ['name' => 'Apple', 'items' => [1, 2, 3]],
    ['name' => 'Banana', 'items' => [1]],
    ['name' => 'Orange', 'items' => [1, 2]],
]);

$sorted = $collection->sortByDesc(fn($item) => count($item['items']));

$sorted->values()->all();
// Output:
// [
//     ['name' => 'Apple', 'items' => [1, 2, 3]],
//     ['name' => 'Orange', 'items' => [1, 2]],
//     ['name' => 'Banana', 'items' => [1]],
// ]

 

Example 3: Sort By Date Desc

$collection = collect([
    ['title' => 'Post A', 'created_at' => '2024-12-10'],
    ['title' => 'Post B', 'created_at' => '2024-12-12'],
    ['title' => 'Post C', 'created_at' => '2024-12-08'],
]);

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

$sorted->values()->all();
// Output:
// [
//     ['title' => 'Post B', 'created_at' => '2024-12-12'],
//     ['title' => 'Post A', 'created_at' => '2024-12-10'],
//     ['title' => 'Post C', 'created_at' => '2024-12-08'],
// ]

 

Example 4: Sort By Desc Multiple Columns

$collection = collect([
    ['name' => 'John', 'score' => 90],
    ['name' => 'John', 'score' => 85],
    ['name' => 'Alice', 'score' => 95],
]);

$sorted = $collection->sortByDesc([
    ['name', SORT_REGULAR],
    ['score', SORT_NUMERIC],
]);

$sorted->values()->all();
// Output:
// [
//     ['name' => 'John', 'score' => 90],
//     ['name' => 'John', 'score' => 85],
//     ['name' => 'Alice', 'score' => 95],
// ]

 

Example 5: Sort By Desc Relation

Assuming you have a collection of posts with a related comments count:

$posts = Post::withCount('comments')->get();

$sorted = $posts->sortByDesc('comments_count');

$sorted->values()->all();
// Output: Posts sorted by comments count in descending order

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS