Laravel 5: Sorting Arrays With sort

April 11, 2018 —John Koster

The sort helper method will allow you to sort the given $array based on some condition returned by the $callback. The method works by iterating over all the values in the $array and passing the values it finds into the $callback function. It then uses the value returned by the callback to determine sort order.

#Signature

The signature of the sort method is:

1public static function sort(
2 $array,
3 $callback = null
4);

#Example Use

Let's refactor our students example from the where section to use a class based structure. First, we will create a new StudentTestResult class:

1class StudentTestResult {
2 
3 public $name = '';
4 
5 public $testScore = '';
6 
7 public function __construct($name, $testScore)
8 {
9 $this->name = $name;
10 $this->testScore = $testScore;
11 }
12 
13}

At this point we can build a new $testScores array:

1$testScores = [
2 new StudentTestResult('Alice', 95),
3 new StudentTestResult('Allison', 96),
4 new StudentTestResult('Bob', 92),
5 new StudentTestResult('Delbert', 72),
6 new StudentTestResult('Dennis', 87),
7 new StudentTestResult('Tonya', 32)
8];

Now, the $testScores array will look like this:

1array {
2 object(StudentTestResult) {
3 ["name"] "Alice"
4 ["testScore"] 95
5 }
6 object(StudentTestResult) {
7 ["name"] "Allison"
8 ["testScore"] 96
9 }
10 object(StudentTestResult) {
11 ["name"] "Bob"
12 ["testScore"] 92
13 }
14 object(StudentTestResult) {
15 ["name"] "Delbert"
16 ["testScore"] 72
17 }
18 object(StudentTestResult) {
19 ["name"] "Dennis"
20 ["testScore"] 87
21 }
22 object(StudentTestResult) {
23 ["name"] "Tonya"
24 ["testScore"] 32
25 }
26}

We want to see the students that have the worst test scores first, so that we can offer them extra help, or just to see what's going on:

1use Illuminate\Support\Arr;
2 
3$sortedScores = Arr::sort($testScores, function($student)
4{
5 // Sort the student's scores by their test score.
6 return $student->testScore;
7});

The $sortedScores array will now look like this:

1array {
2 object(StudentTestResult) {
3 ["name"] "Tonya"
4 ["testScore"] 32
5 }
6 object(StudentTestResult) {
7 ["name"] "Delbert"
8 ["testScore"] 72
9 }
10 object(StudentTestResult) {
11 ["name"] "Dennis"
12 ["testScore"] 87
13 }
14 object(StudentTestResult) {
15 ["name"] "Bob"
16 ["testScore"] 92
17 }
18 object(StudentTestResult) {
19 ["name"] "Alice"
20 ["testScore"] 95
21 }
22 object(StudentTestResult) {
23 ["name"] "Allison"
24 ["testScore"] 96
25 }
26}

Alternatively, we can sort the array based on the student's name:

1use Illuminate\Support\Arr;
2 
3$sortedScores = Arr::sort($testScores, function($student)
4{
5 // Sort the student's scores by their name.
6 return $student->name;
7});

And now the $sortedScores array would look like this:

1array {
2 object(StudentTestResult) {
3 ["name"] "Alice"
4 ["testScore"] 95
5 }
6 object(StudentTestResult) {
7 ["name"] "Allison"
8 ["testScore"] 96
9 }
10 object(StudentTestResult) {
11 ["name"] "Bob"
12 ["testScore"] 92
13 }
14 object(StudentTestResult) {
15 ["name"] "Delbert"
16 ["testScore"] 72
17 }
18 object(StudentTestResult) {
19 ["name"] "Dennis"
20 ["testScore"] 87
21 }
22 object(StudentTestResult) {
23 ["name"] "Tonya"
24 ["testScore"] 32
25 }
26}

A> ## Array Sort Considerations

The array_sort function internally creates a new instance of the "\Illuminate\Support\Collection" class with the given $array and then calls the collection's sortBy(callable $callback = null) method with the given $callback. It then converts the sorted collection into an array by calling the collection's all() method. In short, this function creates a new object every time it is invoked.

Because of this function's internal behavior, code should never be written like the following:

php %% <?php // Create a new collection, using the 'Collect' helper. $collection = collect(['second', 'first', 'third']); $sorted = array_sort($collection->all());

Instead, it is preferable to just use the collection's sortBy method directly:

php %% <?php $collection = collect(['second', 'first', 'third']); $sorted = $collection->sortBy();

#Global array_sort Helper Function

The array_sort function is a shortcut to calling Arr::sort. This function is declared in the global namespace.

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.