How to sort a collection in descending order in illuminate/collections?

To sort a collection in descending order using the Illuminate/Collections library in Laravel, you can use the sortByDesc method. Here's an example:

use Illuminate\Support\Collection; $collection = collect([3, 1, 2, 5, 4]); $sorted = $collection->sortByDesc(function ($item) { return $item; }); $sorted->all(); // returns [5, 4, 3, 2, 1]

In this example, we create a collection with some numbers. We then use the sortByDesc method to sort the collection in descending order based on the values of the items. The $item parameter inside the callback function refers to each item in the collection. Finally, we can use the all method to retrieve the sorted collection as an array.

Note: The sortByDesc method does not modify the original collection. Instead, it returns a new sorted collection.