Eloquent sortBy - Specify Asc or Desc

Trying sort my eloquent collection:

$collection->sortBy('field');

There is no information in Laravel 4's docs on how to choose descending or ascending for this sort method.

Is it possible?

3 Answers

To sort by ASC: Laravel Docs - sortBy()

$collection->sortBy('field');

To sort by DESC: Laravel Docs - sortByDesc()

$collection->sortByDesc('field');

Laravel 5

Laravel 4

Usage

$collection->sortBy('field', [], true); // true for descending
1

In 4.2, the second parameter needs to be an int, SORT_REGULAR is default

$collection->sortBy('field', SORT_REGULAR, true); //true for descending

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest