Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"symfony/mime": "^8.0.0",
"symfony/polyfill-php84": "^1.33",
"symfony/polyfill-php85": "^1.33",
"symfony/polyfill-php86": "^1.36",
"symfony/process": "^7.4.5 || ^8.0.5",
"symfony/routing": "^8.0.0",
"symfony/uid": "^8.0.0",
Expand Down
21 changes: 12 additions & 9 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use InvalidArgumentException;
use JsonSerializable;
use Random\Randomizer;
use SortDirection;
use Traversable;
use WeakMap;

Expand Down Expand Up @@ -1090,7 +1091,7 @@ public static function sole($array, ?callable $callback = null)
* @template TValue
*
* @param iterable<TKey, TValue> $array
* @param callable|string|null|array<int, (callable(TValue, TValue): -1|0|1)|array{string, 'asc'|'desc'}> $callback
* @param callable|string|null|array<int, (callable(TValue, TValue): -1|0|1)|array{string, SortDirection|'asc'|'desc'}> $callback
* @return array<TKey, TValue>
*/
public static function sort($array, $callback = null)
Expand Down Expand Up @@ -1121,7 +1122,7 @@ public static function sortDesc($array, $callback = null)
*
* @param array<TKey, TValue> $array
* @param int-mask-of<SORT_REGULAR|SORT_NUMERIC|SORT_STRING|SORT_LOCALE_STRING|SORT_NATURAL|SORT_FLAG_CASE> $options
* @param bool $descending
* @param SortDirection|bool $descending
* @return array<TKey, TValue>
*/
public static function sortRecursive($array, $options = SORT_REGULAR, $descending = false)
Expand All @@ -1133,13 +1134,15 @@ public static function sortRecursive($array, $options = SORT_REGULAR, $descendin
}

if (! array_is_list($array)) {
$descending
? krsort($array, $options)
: ksort($array, $options);
match ($descending) {
false, SortDirection::Ascending => ksort($array, $options),
true, SortDirection::Descending => krsort($array, $options),
};
} else {
$descending
? rsort($array, $options)
: sort($array, $options);
match ($descending) {
false, SortDirection::Ascending => sort($array, $options),
true, SortDirection::Descending => rsort($array, $options),
};
}

return $array;
Expand All @@ -1158,7 +1161,7 @@ public static function sortRecursive($array, $options = SORT_REGULAR, $descendin
*/
public static function sortRecursiveDesc($array, $options = SORT_REGULAR)
{
return static::sortRecursive($array, $options, true);
return static::sortRecursive($array, $options, SortDirection::Descending);
}

/**
Expand Down
28 changes: 18 additions & 10 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\TransformsToResourceCollection;
use InvalidArgumentException;
use SortDirection;
use stdClass;
use Traversable;

Expand Down Expand Up @@ -1566,7 +1567,7 @@ public function sortDesc($options = SORT_REGULAR)
*
* @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
* @param int $options
* @param bool $descending
* @param SortDirection|bool $descending
* @return static
*/
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
Expand All @@ -1586,8 +1587,10 @@ public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
$results[$key] = $callback($value, $key);
}

$descending ? arsort($results, $options)
: asort($results, $options);
match ($descending) {
false, SortDirection::Ascending => asort($results, $options),
true, SortDirection::Descending => arsort($results, $options),
};

// Once we have sorted all of the keys in the array, we will loop through them
// and grab the corresponding model so we can set the underlying items list
Expand Down Expand Up @@ -1616,15 +1619,17 @@ protected function sortByMany(array $comparisons = [], int $options = SORT_REGUL

$prop = $comparison[0];

$ascending = Arr::get($comparison, 1, true) === true ||
Arr::get($comparison, 1, true) === 'asc';
$direction = match (Arr::get($comparison, 1, SortDirection::Ascending)) {
'asc', SortDirection::Ascending => SortDirection::Ascending,
'desc', SortDirection::Descending => SortDirection::Descending,
};

if (! is_string($prop) && is_callable($prop)) {
$result = $prop($a, $b);
} else {
$values = [data_get($a, $prop), data_get($b, $prop)];

if (! $ascending) {
if ($direction === SortDirection::Descending) {
$values = array_reverse($values);
}

Expand Down Expand Up @@ -1669,7 +1674,7 @@ public function sortByDesc($callback, $options = SORT_REGULAR)
foreach ($callback as $index => $key) {
$comparison = Arr::wrap($key);

$comparison[1] = 'desc';
$comparison[1] = SortDirection::Descending;

$callback[$index] = $comparison;
}
Expand All @@ -1682,14 +1687,17 @@ public function sortByDesc($callback, $options = SORT_REGULAR)
* Sort the collection keys.
*
* @param int $options
* @param bool $descending
* @param SortDirection|bool $descending
* @return static
*/
public function sortKeys($options = SORT_REGULAR, $descending = false)
{
$items = $this->items;

$descending ? krsort($items, $options) : ksort($items, $options);
match ($descending) {
false, SortDirection::Ascending => ksort($items, $options),
true, SortDirection::Descending => krsort($items, $options),
};

return new static($items);
}
Expand All @@ -1702,7 +1710,7 @@ public function sortKeys($options = SORT_REGULAR, $descending = false)
*/
public function sortKeysDesc($options = SORT_REGULAR)
{
return $this->sortKeys($options, true);
return $this->sortKeys($options, SortDirection::Descending);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Collections/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"illuminate/conditionable": "^14.0",
"illuminate/contracts": "^14.0",
"illuminate/macroable": "^14.0",
"symfony/polyfill-php85": "^1.33"
"symfony/polyfill-php85": "^1.33",
"symfony/polyfill-php86": "^1.36"
},
"suggest": {
"illuminate/http": "Required to convert collections to API resources (^14.0).",
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use SortDirection;
use stdClass;
use Symfony\Component\VarDumper\VarDumper;
use UnexpectedValueException;
Expand Down Expand Up @@ -2063,6 +2064,16 @@ public function testSortBy($collection)

$this->assertEquals(['dayle', 'taylor'], array_values($data->all()));

$data = new $collection(['dayle', 'taylor']);
$data = $data->sortBy(
function ($x) {
return $x;
},
SORT_REGULAR,
SortDirection::Descending);

$this->assertEquals(['taylor', 'dayle'], array_values($data->all()));

$data = new $collection(['dayle', 'taylor']);
$data = $data->sortByDesc(function ($x) {
return $x;
Expand Down Expand Up @@ -2153,6 +2164,14 @@ public function testSortByMany($collection)
$data = $data->sortBy([['item', 'desc']]);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

rsort($expected);
$data = $data->sortBy([['item', false]]);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

rsort($expected);
$data = $data->sortBy([['item', SortDirection::Descending]]);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected, SORT_STRING);
$data = $data->sortBy(['item'], SORT_STRING);
$this->assertEquals($data->pluck('item')->toArray(), $expected);
Expand Down
Loading