To sort a collection in ascending order using Illuminate/Collections, you can use the sortBy()
method. Here's an example:
use Illuminate\Support\Collection;
$collection = new Collection([5, 2, 8, 1, 3]);
$sortedCollection = $collection->sortBy(function ($item) {
return $item;
});
$sortedCollection->all();
In this example, the sortBy()
method is used with a callback function that returns the item itself. You can modify the callback function according to your sorting needs. The all()
method is called to retrieve the underlying array of the sorted collection.
The output of the all()
method will be: [1, 2, 3, 5, 8]
which is the sorted collection in ascending order.