Search

Laravel 5 Collections: Sorting a Collection With sort

April 22, 2018 —John Koster

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.

#Signature

1public function sort(
2 callable $callback = null
3);

#Example Use

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

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([8, 4, 6, 2, 7, 5, 1, 3]);
5 
6// Sort the collection.
7$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)
2 protected 'items' =>
3 array
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// Sort the collection in reverse order.
2$sorted = $collection->sort(function($a, $b) {
3 if ($a == $b) {
4 return 0;
5 }
6 
7 return ($a < $b) ? 1 : -1;
8});

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)
2 protected 'items' =>
3 array
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:

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

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)
2 protected 'items' =>
3 array
4 3 =>
5 array
6 'name' => string 'Dave'
7 'age' => int 19
8 0 =>
9 array
10 'name' => string 'Sue'
11 'age' => int 23
12 2 =>
13 array
14 'name' => string 'Jane'
15 'age' => int 25
16 1 =>
17 array
18 'name' => string 'Simon'
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.