Skip to content

Commit fdf1d85

Browse files
【10.x】Individual wrapping in Collection::sortByMany allows sorting with multiple attributes by just an array of attributes(without direction, defaulting to asceding sort), but the spec is missing in docs (#10581)
* Update collections.md * Update collections.md --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent ac2e257 commit fdf1d85

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

collections.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2437,7 +2437,31 @@ Alternatively, you may pass your own closure to determine how to sort the collec
24372437
]
24382438
*/
24392439

2440-
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:
2440+
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:
2441+
2442+
$collection = collect([
2443+
['name' => 'Taylor Otwell', 'age' => 34],
2444+
['name' => 'Abigail Otwell', 'age' => 30],
2445+
['name' => 'Taylor Otwell', 'age' => 36],
2446+
['name' => 'Abigail Otwell', 'age' => 32],
2447+
]);
2448+
2449+
$sorted = $collection->sortBy(['name', 'age']);
2450+
2451+
$sorted->values()->all();
2452+
2453+
/*
2454+
[
2455+
['name' => 'Abigail Otwell', 'age' => 30],
2456+
['name' => 'Abigail Otwell', 'age' => 32],
2457+
['name' => 'Taylor Otwell', 'age' => 34],
2458+
['name' => 'Taylor Otwell', 'age' => 36],
2459+
]
2460+
*/
2461+
2462+
2463+
2464+
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:
24412465

24422466
$collection = collect([
24432467
['name' => 'Taylor Otwell', 'age' => 34],

0 commit comments

Comments
 (0)