Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -2437,7 +2437,31 @@ Alternatively, you may pass your own closure to determine how to sort the collec
]
*/

If you would like to sort your collection by multiple attributes, you may pass an array of sort operations to the `sortBy` method. Each sort operation should be an array consisting of the attribute that you wish to sort by and the direction of the desired sort:
If you would like to sort your collection by multiple attributes, you may pass an array of the attributes that you wish to sort by:

$collection = collect([
['name' => 'Taylor Otwell', 'age' => 34],
['name' => 'Abigail Otwell', 'age' => 30],
['name' => 'Taylor Otwell', 'age' => 36],
['name' => 'Abigail Otwell', 'age' => 32],
]);

$sorted = $collection->sortBy(['name', 'age']);

$sorted->values()->all();

/*
[
['name' => 'Abigail Otwell', 'age' => 30],
['name' => 'Abigail Otwell', 'age' => 32],
['name' => 'Taylor Otwell', 'age' => 34],
['name' => 'Taylor Otwell', 'age' => 36],
]
*/



When sorting in multiple attributes and directions, you may pass an array of sort operations to the `sortBy` method. Each sort operation should be an array consisting of the attribute that you wish to sort by and the direction of the desired sort:

$collection = collect([
['name' => 'Taylor Otwell', 'age' => 34],
Expand Down