Published in Laravel

Laravel Collection Public API: sortByDesc

By John Koster

sortByDesc($callback, $options = SORT_REGULAR)

The sortByDesc method operates exactly the same as the sortBy method. The only differences is that the sortByDesc method does not define a $descending parameter. The sortByDesc method internally makes a call to the sortBy method passing true as the argument for the $descending parameter.

The following calls to the sortBy and sortByDesc methods are functionally equivalent:

{lang="php",line-numbers=on}

1<?php
2
3use Illuminate\Support\Collection;
4
5// Create a new collection instance.
6$collection = new Collection([
7 ['name' => 'Marshall']
8]);
9
10$sorted = $collection->sortBy('name', SORT_REGULAR, true);
11
12$sorted = $collection->sortByDesc('name', SORT_REGULAR);
13
14$sorted = $collection->sortByDesc('name');