Skip to content

Commit

Permalink
Add sortRecursive and sortRecursiveDesc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Narek Melkonyan committed Jan 16, 2024
1 parent c69c514 commit c933faf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1366,30 +1366,26 @@ public function sortDesc($options = SORT_REGULAR)
}

/**
* Recursively sort the collection by keys and values.
* Recursively sort an array by keys and values.
*
* @param int $options
* @param bool $descending
* @param int $options
* @param bool $descending
* @return static
*/
public function sortRecursive($options = SORT_REGULAR, $descending = false)
{
$items = $this->items;

return new static(Arr::sortRecursive($items, $options = SORT_REGULAR, $descending = false));
return new static(Arr::sortRecursive($this->items, $options = SORT_REGULAR, $descending = false));
}

/**
* Recursively sort the collection by keys and values in descending order.
* Recursively sort an array by keys and values in descending order.
*
* @param int $options
* @param int $options
* @return static
*/
public function sortRecursiveDesc($options = SORT_REGULAR)
{
$items = $this->items;

return new static(Arr::sortRecursiveDesc($items, $options = SORT_REGULAR));
return new static(Arr::sortRecursiveDesc($this->items, $options = SORT_REGULAR));
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,29 @@ public function sortDesc($options = SORT_REGULAR)
return $this->passthru('sortDesc', func_get_args());
}

/**
* Recursively sort an array by keys and values.
*
* @param int $options
* @param bool $descending
* @return static
*/
public function sortRecursive($options = SORT_REGULAR, $descending = false)
{
return $this->passthru('sortRecursive', func_get_args());
}

/**
* Recursively sort an array by keys and values in descending order.
*
* @param int $options
* @return static
*/
public function sortRecursiveDesc($options = SORT_REGULAR)
{
return $this->passthru('sortRecursiveDesc', func_get_args());
}

/**
* Sort the collection using the given callback.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,28 @@ public function testSortIsLazy()
});
}

public function testLazySortRecursiveDescIsLazy(): void
{
$this->assertDoesNotEnumerate(function ($collection) {
$collection->sortRecursiveDesc();
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->sortRecursiveDesc()->all();
});
}

public function testLazySortRecursiveIsLazy(): void
{
$this->assertDoesNotEnumerate(function ($collection) {
$collection->sortRecursive();
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->sortRecursive()->all();
});
}

public function testSortDescIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
Expand Down

0 comments on commit c933faf

Please sign in to comment.