1$collection = collect([5, 3, 1, 2, 4]);
2
3$sorted = $collection->sortDesc();
4
5$sorted->values()->all();
6
7// [5, 4, 3, 2, 1]
1$collection = collect([
2 ['name' => 'Taylor Otwell', 'age' => 34],
3 ['name' => 'Abigail Otwell', 'age' => 30],
4 ['name' => 'Taylor Otwell', 'age' => 36],
5 ['name' => 'Abigail Otwell', 'age' => 32],
6]);
7
8$sorted = $collection->sortBy([
9 fn ($a, $b) => $a['name'] <=> $b['name'],
10 fn ($a, $b) => $b['age'] <=> $a['age'],
11]);
12
13$sorted->values()->all();
14
15/*
16 [
17 ['name' => 'Abigail Otwell', 'age' => 32],
18 ['name' => 'Abigail Otwell', 'age' => 30],
19 ['name' => 'Taylor Otwell', 'age' => 36],
20 ['name' => 'Taylor Otwell', 'age' => 34],
21 ]
22*/
1$collection = collect([
2 ['name' => 'Desk', 'price' => 200],
3 ['name' => 'Chair', 'price' => 100],
4 ['name' => 'Bookcase', 'price' => 150],
5]);
6
7$sorted = $collection->sortBy('price');
8
9$sorted->values()->all();
10
11/*
12 [
13 ['name' => 'Chair', 'price' => 100],
14 ['name' => 'Bookcase', 'price' => 150],
15 ['name' => 'Desk', 'price' => 200],
16 ]
17*/