In this post titled Laravel 12 SortBy Collection Tutorial with Example, you’ll learn how to sort collection data in multiple ways, including by key, nested key, dates, and more — all through easy-to-follow examples.
Sorting data efficiently is a common requirement in any web application, and Laravel makes it incredibly simple with its powerful Collection methods. In this post, we’ll walk you through multiple practical examples of using the sortBy() method to sort collections in Laravel.
You’ll learn step-by-step how to use sortBy() in various scenarios, including:
- Laravel Collection: sortBy Null Last
- Laravel Collection: Sort by Key
- Laravel Collection: Sort by Multiple Columns
- Laravel Collection: sortBy Using Callback Function
- Laravel Collection: sortBy Nested Key
- Laravel Collection: sortBy Date Example
By the end of this guide, you’ll have a good understanding of how to sort Laravel collections in multiple .
1. Laravel Collection: sortBy null Last
//laravel collection sortby null value in last
$collection = collect([
['product' => 'product 1', 'created_at' => '2025-12-05'],
['product' => 'product 2', 'created_at' => '2025-07-30'],
['product' => 'product 3', 'created_at' => '2025-11-12'],
['product' => 'product 4', 'created_at' => ''],
]);
$sorted = $collection->sortBy(function(array $item){
return $item['created_at'] ?? !is_null($item['created_at']) ;
});
$sorted->values()->all();
/*
[
['product' => 'product 2', 'created_at' => '2025-07-30'],
['product' => 'product 3', 'created_at' => '2025-11-12'],
['product' => 'product 1', 'created_at' => '2025-12-05'],
['product' => 'product 4', 'created_at' => ''],
]
*/
dd($sorted);
}
The output is:
Illuminate\Support\Collection {#262 ▼ // app\Http\Controllers\DemoController.php:32
#items: array:4 [▼
3 => array:2 [▼
"product" => "product 4"
"created_at" => ""
]
1 => array:2 [▼
"product" => "product 2"
"created_at" => "2025-07-30"
]
2 => array:2 [▼
"product" => "product 3"
"created_at" => "2025-11-12"
]
0 => array:2 [▼
"product" => "product 1"
"created_at" => "2025-12-05"
]
]
#escapeWhenCastingToString: false
}
2. Laravel Collection: Sort by Key
The sortBy method sorts a collection based on the specified key. Since the original array keys are preserved after sorting, we use the values method afterward to reindex the collection with consecutive numeric keys.
public function ItemSort()
{ //sort by value
$collection = collect([
['product' => 'Pillow Cover', 'price' => 500],
['product' => 'Hand Bag', 'price' => 600],
['product' => 'Book', 'price' => 100],
]);
$sorted = $collection->sortBy('price');
$sorted->values()->all();
dd($sorted);
}
The Output is:
Illuminate\Support\Collection {#262 ▼ // app\Http\Controllers\DemoController.php:19
#items: array:3 [▼
2 => array:2 [▼
"product" => "Book"
"price" => 100
]
0 => array:2 [▼
"product" => "Pillow Cover"
"price" => 500
]
1 => array:2 [▼
"product" => "Hand Bag"
"price" => 600
]
]
#escapeWhenCastingToString: false
}
The sortBy method also accepts sort flags as an optional second argument:
public function ItemSort()
{ //Use the SORT_NATURAL flag with sort() to sort names containing numbers in a natural order.
$collection = collect([
['product' => 'Book 1','price'=>200],
['product' => 'Book 10','price'=>300],
['product' => 'Book 5','price'=>250],
['product' => 'Book 12','price'=>100],
]);
$sorted = $collection->sortBy('product', SORT_NATURAL);
$sorted->values()->all();
dd($sorted);
}
The array was sorted by the name key, in alphabetical order, using natural sorting, which takes both letters and numbers into account in a human-friendly way. The output is:
Illuminate\Support\Collection {#262 ▼ // app\Http\Controllers\DemoController.php:22
#items: array:4 [▼
0 => array:2 [▼
"product" => "Book 1"
"price" => 200
]
2 => array:2 [▼
"product" => "Book 5"
"price" => 250
]
1 => array:2 [▼
"product" => "Book 10"
"price" => 300
]
3 => array:2 [▼
"product" => "Book 12"
"price" => 100
]
]
#escapeWhenCastingToString: false
}
3. Laravel Collection: Sort by Multiple Columns
public function index()
{ //sort multiple columns
$collection = collect([
['name' => 'Math', 'marks' => 90],
['name' => 'Science', 'marks' => 80],
['name' => 'Physics', 'marks' => 96],
['name' => 'Chemistry', 'marks' => 89],
]);
$sorted = $collection->sortBy([
['name', 'asc'],
['marks', 'desc'],
]);
$sorted->values()->all();
dd($sorted);
}
//output is
// ["name" => "Chemistry","marks" => 89]
// ["name" => "Math" , "marks" => 90]
// ["name" => "Physics", "marks" => 96]
// ["name" => "Science" ,"marks" => 80]
In this example “Laravel Collection Sort by Multiple Columns” , it sorts name in ascending alphabetical order. The output is:
Illuminate\Support\Collection {#258 ▼ // app\Http\Controllers\DemoController.php:25
#items: array:4 [▼
3 => array:2 [▼
"name" => "Chemistry"
"marks" => 89
]
0 => array:2 [▼
"name" => "Math"
"marks" => 90
]
2 => array:2 [▼
"name" => "Physics"
"marks" => 96
]
1 => array:2 [▼
"name" => "Science"
"marks" => 80
]
]
#escapeWhenCastingToString: false
}
4. Laravel Collection: sortBy Using Callback Function
The sortBy method allows you to pass a custom callback function (closure) to define how the collection should be sorted. In this example, we’re using count to determine the number of products and sort the collection in ascending order based on the product count. You can customize the callback with your own sorting logic as needed.
//sort using callback function
$collection = collect([
['price' => 1000, 'product' => ['bag', 'shirt']],
['price' => 500, 'product' => ['orange', 'apple', 'banana']],
['price' => 200, 'product' => ['Bag']],
]);
$sorted = $collection->sortBy(function (array $product, int $key) {
return count($product['product']);
});
$sorted->values()->all();
/*
[
['price' => 200, 'product' => ['Bag']],
['price' => 1000, 'product' => ['bag', 'shirt']],
['price' => 500, 'product' => ['orange', 'apple', 'banana']],
]
*/
dd($sorted);
}
the output is:
Illuminate\Support\Collection {#262 ▼ // app\Http\Controllers\DemoController.php:31
#items: array:3 [▼
2 => array:2 [▼
"price" => 200
"product" => array:1 [▼
0 => "Bag"
]
]
0 => array:2 [▼
"price" => 1000
"product" => array:2 [▼
0 => "bag"
1 => "shirt"
]
]
1 => array:2 [▼
"price" => 500
"product" => array:3 [▼
0 => "orange"
1 => "apple"
2 => "banana"
]
]
]
#escapeWhenCastingToString: false
}
5. Laravel Collection: Sortby Nested Key
public function index()
{ //sortBy Nested Key
$collection = collect([
['marks' => 1000, 'id'=>1, 'info' => ['name'=>"Jhon"]],
['marks' => 500, 'id'=>2, 'info' => ['name'=>"Dany"]],
['marks' => 200, 'id'=>3, 'info' => ['name'=>"Any"]],
]);
$sorted = $collection->sortBy('info.name');
$sorted->values()->all();
/*
[
['marks' => 200, 'id'=>3, 'info' => ['name'=>"Any"]],
['marks' => 500, 'id'=>2, 'info' => ['name'=>"Dany"]],
['marks' => 1000, 'id'=>1, 'info' => ['name'=>"Jhon"]],
]
*/
dd($sorted);
}
The output is:
Illuminate\Support\Collection {#262 ▼ // app\Http\Controllers\DemoController.php:29
#items: array:3 [▼
2 => array:3 [▼
"marks" => 200
"id" => 3
"info" => array:1 [▼
"name" => "Any"
]
]
1 => array:3 [▼
"marks" => 500
"id" => 2
"info" => array:1 [▼
"name" => "Dany"
]
]
0 => array:3 [▼
"marks" => 1000
"id" => 1
"info" => array:1 [▼
"name" => "Jhon"
]
]
]
#escapeWhenCastingToString: false
}
6. Laravel Collection: sortBy Date Example
public function index()
{ //sortBy date
$collection = collect([
['product' => 'product 1', 'created_at' => '2025-12-05'],
['product' => 'product 2', 'created_at' => '2025-07-30'],
['product' => 'product 3', 'created_at' => '2025-11-12'],
]);
$sorted = $collection->sortBy('created_at');
$sorted->values()->all();
/*
[
['product' => 'product 2', 'created_at' => '2025-07-30'],
['product' => 'product 3', 'created_at' => '2025-11-12'],
['product' => 'product 1', 'created_at' => '2025-12-05'],
]
*/
dd($sorted);
}
The output is:
Illuminate\Support\Collection {#262 ▼ // app\Http\Controllers\DemoController.php:29
#items: array:3 [▼
1 => array:2 [▼
"product" => "product 2"
"created_at" => "2025-07-30"
]
2 => array:2 [▼
"product" => "product 3"
"created_at" => "2025-11-12"
]
0 => array:2 [▼
"product" => "product 1"
"created_at" => "2025-12-05"
]
]
#escapeWhenCastingToString: false
}
7. Laravel Collection: Sort By Relation
In this example, we are using a members table and a posts table, where one member has a one-to-many relationship with posts.


In this example, we are using a members table and a posts table, where one member has a one-to-many relationship with posts.
public function index()
{ //laravel collection sortby null last
$post_list = Post::get(['title','user_id'])->sortBy(function($qry){
return $qry->member->name;
})->toArray();
dd($post_list);
}
The output is:

I hope these examples help you better understand how to use sortBy() in Laravel collections.