sort($array, callable $callback)
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.
Let's refactor our students example (from previous articles) to use a class based structure. First, we will create a new StudentTestResult
class:
1<?php
2
3class StudentTestResult {
4
5 public $name = '';
6
7 public $testScore = '';
8
9 public function __construct($name, $testScore)
10 {
11 $this->name = $name;
12 $this->testScore = $testScore;
13 }
14
15}
At this point we can build a new $testScores
array:
1<?php
2
3$testScores = [
4 new StudentTestResult('Alice', 95),
5 new StudentTestResult('Allison', 96),
6 new StudentTestResult('Bob', 92),
7 new StudentTestResult('Delbert', 72),
8 new StudentTestResult('Dennis', 87),
9 new StudentTestResult('Tonya', 32)
10];
Now, the $testScores
array will look like this:
1array(6) {
2 [0] object(StudentTestResult)#138 (2) {
3 ["name"] "Alice"
4 ["testScore"] 95
5 }
6 [1] object(StudentTestResult)#139 (2) {
7 ["name"] "Allison"
8 ["testScore"] 96
9 }
10 [2] object(StudentTestResult)#140 (2) {
11 ["name"] "Bob"
12 ["testScore"] 92
13 }
14 [3] object(StudentTestResult)#141 (2) {
15 ["name"] "Delbert"
16 ["testScore"] 72
17 }
18 [4] object(StudentTestResult)#142 (2) {
19 ["name"] "Dennis"
20 ["testScore"] 87
21 }
22 [5] object(StudentTestResult)#143 (2) {
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:
1<?php
2
3use Illuminate\Support\Arr;
4
5$sortedScores = Arr::sort($testScores, function($student)
6{
7 // Sort the student's scores by their test score.
8 return $student->testScore;
9});
The $sortedScores
array will now look like this:
1array(6) {
2 [5] object(StudentTestResult)#143 (2) {
3 ["name"] "Tonya"
4 ["testScore"] 32
5 }
6 [3] object(StudentTestResult)#141 (2) {
7 ["name"] "Delbert"
8 ["testScore"] 72
9 }
10 [4] object(StudentTestResult)#142 (2) {
11 ["name"] "Dennis"
12 ["testScore"] 87
13 }
14 [2] object(StudentTestResult)#140 (2) {
15 ["name"] "Bob"
16 ["testScore"] 92
17 }
18 [0] object(StudentTestResult)#138 (2) {
19 ["name"] "Alice"
20 ["testScore"] 95
21 }
22 [1] object(StudentTestResult)#139 (2) {
23 ["name"] "Allison"
24 ["testScore"] 96
25 }
26}
Alternatively, we can sort the array based on the student's name:
1<?php
2
3use Illuminate\Support\Arr;
4
5$sortedScores = Arr::sort($testScores, function($student)
6{
7 // Sort the student's scores by their name.
8 return $student->name;
9});
And now the $sortedScores
array would look like this:
1array(6) {
2 [0] object(StudentTestResult)#138 (2) {
3 ["name"] "Alice"
4 ["testScore"] 95
5 }
6 [1] object(StudentTestResult)#139 (2) {
7 ["name"] "Allison"
8 ["testScore"] 96
9 }
10 [2] object(StudentTestResult)#140 (2) {
11 ["name"] "Bob"
12 ["testScore"] 92
13 }
14 [3] object(StudentTestResult)#141 (2) {
15 ["name"] "Delbert"
16 ["testScore"] 72
17 }
18 [4] object(StudentTestResult)#142 (2) {
19 ["name"] "Dennis"
20 ["testScore"] 87
21 }
22 [5] object(StudentTestResult)#143 (2) {
23 ["name"] "Tonya"
24 ["testScore"] 32
25 }
26}
#array_sort($array, callable $callback)
The array_sort
function is a shortcut to calling Arr::sort
. This function is declared in the global namespace.
∎