How to sort laravel collection by different methods

In this tutorial we will discuss sorting laravel collection by different methods. how to sort laravel collection array. how to sort laravel Multidimensional collection sortBy method.


Laravel collection different sorting methods

1. sort()
1. sortBy()
1. sortByDesc()
1. sortDesc()
1. sortKeys()
1. sortKeysDesc()
1. sortKeysUsing()

Laravel collection sort() method

The sort method sorts the collection items and its sorted collection keep original array keys so we use value method to reset keys.

$collection = collect([5, 3, 1, 2, 4]);
 
$sorted = $collection->sort();
 
dd($sorted->values());
 
// [1, 2, 3, 4, 5]

Laravel collection sortBy() method

laravel sortBy method sorts collection by key name. Here sorted collection keep the original array keys so we reset it by values() method.

$collection = collect([
  ['name' => 'Krishan', 'age' => 28],
  ['name' => 'Anmol', 'age' => 20],
  ['name' => 'Alok', 'age' => 25],
]);
 
$sorted = $collection->sortBy('age');
$sorted->values()->all();
 
/*
  [
    ['name' => 'Anmol', 'age' => 20],
    ['name' => 'Alok', 'age' => 25],
    ['name' => 'Krishan', 'age' => 28],
  ]
*/

The sortBy method also include sort flags as second argument see below example.

$collection = collect([
  ['title' => 'Item 1'],
  ['title' => 'Item 12'],
  ['title' => 'Item 3'],
]);
 
$sorted = $collection->sortBy('title', SORT_NATURAL);
$sorted->values()->all();
 
/*
  [
    ['title' => 'Item 1'],
    ['title' => 'Item 3'],
    ['title' => 'Item 12'],
  ]
*/

You can also sort an collection array by multiple keys

$collection = collect([
  ['name' => 'Krishan', 'age' => 28],
  ['name' => 'Anmol', 'age' => 20],
  ['name' => 'Alok', 'age' => 25],
  ['name' => 'Anmol', 'age' => 22],
]);
 
$sorted = $collection->sortBy([
  ['name', 'asc'],
  ['age', 'desc'],
]);
$sorted->values()->all();
 
/*
  [
    ['name' => 'Alok', 'age' => 25],
    ['name' => 'Anmol', 'age' => 20],
    ['name' => 'Anmol', 'age' => 22],
    ['name' => 'Krishan', 'age' => 28],
  ]
*/

Laravel collection sortByDesc() method

Laravel collection sortByDesc() method same work as sortBy() but in opposite order. It sort data in descending order.

Laravel collection sortDesc() method

This method is sort data in opposite order of sort() method.

$collection = collect([5, 3, 1, 2, 4]);
 
$sorted = $collection->sortDesc();
 
$sorted->values()->all();
 
// [5, 4, 3, 2, 1]

Laravel collection sortKeys() method

Laravel sortKeys() method sort the data by collection keys name in associative array.

$collection = collect([
  'id' => 15,
  'first' => 'Anmol',
  'last' => 'Sharma',
]);
 
$sorted = $collection->sortKeys();
$sorted->all();
 
/*
  [
    'first' => 'John',
    'id' => 22345,
    'last' => 'Doe',
  ]
*/

Laravel collection sortKeysDesc() method

This method is opposite of sortKeys()

Laravel collection sortKeysUsing() method

Laravel collection sortKeysUsing() method sort the collection by keys with an associative array using the callback.

$collection = collect([
  'ID' => 22345,
  'first' => 'Anmol',
  'last' => 'Sharma',
]);
 
$sorted = $collection->sortKeysUsing('strnatcasecmp');
$sorted->all();
 
/*
  [
    'first' => 'Anmol',
    'ID' => 22345,
    'last' => 'Sharma',
  ]
*/
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Random tutorial links