99use Illuminate \Support \Traits \Macroable ;
1010use Illuminate \Support \Traits \TransformsToResourceCollection ;
1111use InvalidArgumentException ;
12+ use SortDirection ;
1213use stdClass ;
1314use Traversable ;
1415
@@ -1566,7 +1567,7 @@ public function sortDesc($options = SORT_REGULAR)
15661567 *
15671568 * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
15681569 * @param int $options
1569- * @param bool $descending
1570+ * @param SortDirection| bool $descending
15701571 * @return static
15711572 */
15721573 public function sortBy ($ callback , $ options = SORT_REGULAR , $ descending = false )
@@ -1586,8 +1587,10 @@ public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
15861587 $ results [$ key ] = $ callback ($ value , $ key );
15871588 }
15881589
1589- $ descending ? arsort ($ results , $ options )
1590- : asort ($ results , $ options );
1590+ match ($ descending ) {
1591+ false , SortDirection::Ascending => asort ($ results , $ options ),
1592+ true , SortDirection::Descending => arsort ($ results , $ options ),
1593+ };
15911594
15921595 // Once we have sorted all of the keys in the array, we will loop through them
15931596 // and grab the corresponding model so we can set the underlying items list
@@ -1616,15 +1619,18 @@ protected function sortByMany(array $comparisons = [], int $options = SORT_REGUL
16161619
16171620 $ prop = $ comparison [0 ];
16181621
1619- $ ascending = Arr::get ($ comparison , 1 , true ) === true ||
1620- Arr::get ($ comparison , 1 , true ) === 'asc ' ;
1622+ $ direction = match (Arr::get ($ comparison , 1 , true )) {
1623+ true , 'asc ' , SortDirection::Ascending => SortDirection::Ascending,
1624+ false , 'desc ' , SortDirection::Descending => SortDirection::Descending,
1625+ default => SortDirection::Descending, // for backwards compatibility
1626+ };
16211627
16221628 if (! is_string ($ prop ) && is_callable ($ prop )) {
16231629 $ result = $ prop ($ a , $ b );
16241630 } else {
16251631 $ values = [data_get ($ a , $ prop ), data_get ($ b , $ prop )];
16261632
1627- if (! $ ascending ) {
1633+ if ($ direction === SortDirection::Descending ) {
16281634 $ values = array_reverse ($ values );
16291635 }
16301636
@@ -1669,7 +1675,7 @@ public function sortByDesc($callback, $options = SORT_REGULAR)
16691675 foreach ($ callback as $ index => $ key ) {
16701676 $ comparison = Arr::wrap ($ key );
16711677
1672- $ comparison [1 ] = ' desc ' ;
1678+ $ comparison [1 ] = SortDirection::Descending ;
16731679
16741680 $ callback [$ index ] = $ comparison ;
16751681 }
@@ -1682,14 +1688,17 @@ public function sortByDesc($callback, $options = SORT_REGULAR)
16821688 * Sort the collection keys.
16831689 *
16841690 * @param int $options
1685- * @param bool $descending
1691+ * @param SortDirection| bool $descending
16861692 * @return static
16871693 */
16881694 public function sortKeys ($ options = SORT_REGULAR , $ descending = false )
16891695 {
16901696 $ items = $ this ->items ;
16911697
1692- $ descending ? krsort ($ items , $ options ) : ksort ($ items , $ options );
1698+ match ($ descending ) {
1699+ false , SortDirection::Ascending => ksort ($ items , $ options ),
1700+ true , SortDirection::Descending => krsort ($ items , $ options ),
1701+ };
16931702
16941703 return new static ($ items );
16951704 }
@@ -1702,7 +1711,7 @@ public function sortKeys($options = SORT_REGULAR, $descending = false)
17021711 */
17031712 public function sortKeysDesc ($ options = SORT_REGULAR )
17041713 {
1705- return $ this ->sortKeys ($ options , true );
1714+ return $ this ->sortKeys ($ options , SortDirection::Descending );
17061715 }
17071716
17081717 /**
0 commit comments