Skip to content

Commit 2f86d4e

Browse files
authored
Sort collections by key when first element of sort operation is string (even if callable) (#40212)
* Sort collections by key when given a string even when key is callable * Fix style (minor) * Add test for sorting by callable string * Fix style (minor)
1 parent 676ba54 commit 2f86d4e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ protected function sortByMany(array $comparisons = [])
13191319

13201320
$result = 0;
13211321

1322-
if (is_callable($prop)) {
1322+
if (! is_string($prop) && is_callable($prop)) {
13231323
$result = $prop($a, $b);
13241324
} else {
13251325
$values = [data_get($a, $prop), data_get($b, $prop)];

tests/Support/SupportCollectionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,17 @@ public function testSortByString($collection)
18281828
$this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
18291829
}
18301830

1831+
/**
1832+
* @dataProvider collectionClassProvider
1833+
*/
1834+
public function testSortByCallableString($collection)
1835+
{
1836+
$data = new $collection([['sort' => 2], ['sort' => 1]]);
1837+
$data = $data->sortBy([['sort', 'asc']]);
1838+
1839+
$this->assertEquals([['sort' => 1], ['sort' => 2]], array_values($data->all()));
1840+
}
1841+
18311842
/**
18321843
* @dataProvider collectionClassProvider
18331844
*/

0 commit comments

Comments
 (0)