laravel/framework — pull request #3124 by phpmycoder (2014-01-04 14:36:40+00:00)
Title: [Proposal] Add descending order for Collection::sortBy

When scouring through the source for `Collection`, I couldn't find a descending sort function (both `sort` and `sortBy` use `asort` internally). There are two alternatives:

$collection->sortBy(function($item) { return PHP_INT_MAX - $item->sortableColumn });
// or
$collection->sortBy(function($item) { return $item->sortableColumn; })->reverse();

The first is mediocre (albeit not very explicit about its behavior), and the second performs poorly (PHP internally traverses the array at least twice) and creates a new collection.

I propose adding an optional argument to the sort functions that reverses order:

$collection->sortBy(function($item) { return $item->sortableColumn; }, true);

If an optional argument isn't explicit enough, perhaps `sortByDescending` and `sortDescending` functions would be more suitable.