Laravel Collection Public API: sort

November 30, 2016 —John Koster

sort(callable $callback = null)

The sort method is used to sort the collection. If no $callback is provided, the collection will be sorted using a case-insensitive "natural order" algorithm. An optional $callback can be supplied to customize the comparison that is used when sorting the collection's items. The sort method does not affect the order of the original collection, but rather returns a new instance of Collection.

The following example highlights the use of the sort method without supplying a user-defined comparison function (by providing an argument for $callback):

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([8, 4, 6, 2, 7, 5, 1, 3]);
7 
8// Sort the collection.
9$sorted = $collection->sort();

After the above code has executed, the $sorted variable will be an instance of Collection and contain a value similar to the following output (notice the order of the collection's items):

1object(Illuminate\Support\Collection)[134]
2 protected 'items' =>
3 array (size=8)
4 6 => int 1
5 3 => int 2
6 7 => int 3
7 1 => int 4
8 5 => int 5
9 2 => int 6
10 4 => int 7
11 0 => int 8

It should also be noted that the sort method preserves the keys of the collection items when sorting.

#Custom Sorting Comparison Function

The $callback parameter can be utilized to provide a custom sorting comparison function. The $callback should define two parameters, which are typically $a and $b. The $callback must return an integer that is less than, equal to or greater than zero. If the $callback returns 0, the arguments $a and $b are considered to be equal. If the $callback returns a value less than 0, $a is considered to be less than $b, and if $callback returns a value greater than 0, $a is considered to be greater than $b.

The following example will use the $collection created earlier and demonstrate how to reverse the sorting order:

1<?php
2 
3// Sort the collection in reverse order.
4$sorted = $collection->sort(function($a, $b) {
5 if ($a == $b) {
6 return 0;
7 }
8 
9 return ($a < $b) ? 1 : -1;
10});

After the above code has executed, the $sorted variable will be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)[135]
2 protected 'items' =>
3 array (size=8)
4 0 => int 8
5 4 => int 7
6 2 => int 6
7 5 => int 5
8 1 => int 4
9 7 => int 3
10 3 => int 2
11 6 => int 1

A more practical example would be to sort a collection of people based on their age:

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([
7 ['name' => 'Sue', 'age' => 23],
8 ['name' => 'Simon', 'age' => 38],
9 ['name' => 'Jane', 'age' => 25],
10 ['name' => 'Dave', 'age' => 19]
11]);
12 
13// Sort the collection based on a person's age.
14$sorted = $collection->sort(function($a, $b) {
15 if ($a['age'] == $b['age']) {
16 return 0;
17 }
18 
19 return ($a['age'] < $b['age']) ? -1 : 1;
20});

After the above code has executed, the $sorted variable will be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)[135]
2 protected 'items' =>
3 array (size=4)
4 3 =>
5 array (size=2)
6 'name' => string 'Dave' (length=4)
7 'age' => int 19
8 0 =>
9 array (size=2)
10 'name' => string 'Sue' (length=3)
11 'age' => int 23
12 2 =>
13 array (size=2)
14 'name' => string 'Jane' (length=4)
15 'age' => int 25
16 1 =>
17 array (size=2)
18 'name' => string 'Simon' (length=5)
19 'age' => int 38

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.