From bcbc6f1d75f5e85ac7ec89fdca95f967e9c671b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Thu, 4 Jul 2024 18:46:08 +0800 Subject: [PATCH] Revert "Fix sortByMany return is not same to sortBy & Perf sortBy (#6925)" (#6930) This reverts commit e76d97c10eebda5408a1a00924fb84ce647512e1. --- CHANGELOG-3.1.md | 4 - src/collection/src/Collection.php | 34 +---- src/collection/tests/CollectionTest.php | 181 ------------------------ 3 files changed, 5 insertions(+), 214 deletions(-) diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index 8fc8f1ca2..1f0b23092 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -2,10 +2,6 @@ # v3.1.29 - 2024-07-04 -## Fixed - -- [#6925](https://github.com/hyperf/hyperf/pull/6925) Fixed bug that `sortByMany` cannot support options like `sortBy`. - ## Added - [#6896](https://github.com/hyperf/hyperf/pull/6896) Added `SftpAdapter` for `hyperf/filesystem`. diff --git a/src/collection/src/Collection.php b/src/collection/src/Collection.php index e13fb149c..7bd40bea3 100644 --- a/src/collection/src/Collection.php +++ b/src/collection/src/Collection.php @@ -1088,7 +1088,7 @@ class Collection implements Enumerable, ArrayAccess public function sortBy($callback, int $options = SORT_REGULAR, bool $descending = false): static { if (is_array($callback) && ! is_callable($callback)) { - return $this->sortByMany($callback, $options); + return $this->sortByMany($callback); } $results = []; @@ -1112,21 +1112,11 @@ class Collection implements Enumerable, ArrayAccess /** * Sort the collection in descending order using the given callback. * - * @param array|(callable(TValue, TKey): mixed)|string $callback + * @param (callable(TValue, TKey): mixed)|string $callback * @return static */ public function sortByDesc($callback, int $options = SORT_REGULAR): static { - if (is_array($callback) && ! is_callable($callback)) { - foreach ($callback as $index => $key) { - $comparison = Arr::wrap($key); - - $comparison[1] = 'desc'; - - $callback[$index] = $comparison; - } - } - return $this->sortBy($callback, $options, true); } @@ -1599,11 +1589,11 @@ class Collection implements Enumerable, ArrayAccess * * @return static */ - protected function sortByMany(array $comparisons = [], int $options = SORT_REGULAR) + protected function sortByMany(array $comparisons = []) { $items = $this->items; - uasort($items, function ($a, $b) use ($comparisons, $options) { + usort($items, function ($a, $b) use ($comparisons) { foreach ($comparisons as $comparison) { $comparison = Arr::wrap($comparison); @@ -1621,21 +1611,7 @@ class Collection implements Enumerable, ArrayAccess $values = array_reverse($values); } - if (($options & SORT_FLAG_CASE) === SORT_FLAG_CASE) { - if (($options & SORT_NATURAL) === SORT_NATURAL) { - $result = strnatcasecmp($values[0], $values[1]); - } else { - $result = strcasecmp($values[0], $values[1]); - } - } else { - $result = match ($options) { - SORT_NUMERIC => intval($values[0]) <=> intval($values[1]), - SORT_STRING => strcmp($values[0], $values[1]), - SORT_NATURAL => strnatcmp($values[0], $values[1]), - SORT_LOCALE_STRING => strcoll($values[0], $values[1]), - default => $values[0] <=> $values[1], - }; - } + $result = $values[0] <=> $values[1]; } if ($result === 0) { diff --git a/src/collection/tests/CollectionTest.php b/src/collection/tests/CollectionTest.php index 59f95befd..51773792e 100644 --- a/src/collection/tests/CollectionTest.php +++ b/src/collection/tests/CollectionTest.php @@ -1100,185 +1100,4 @@ class CollectionTest extends TestCase $c = new $collection(['foo' => 'bar', 1, 2, 3, 4, 5]); $this->assertNull($c->after(5)); } - - #[DataProvider('collectionClassProvider')] - public function testSortBy($collection) - { - $data = (new $collection( - [ - ['id' => 5, 'name' => 'e'], - ['id' => 4, 'name' => 'd'], - ['id' => 3, 'name' => 'c'], - ['id' => 2, 'name' => 'b'], - ['id' => 1, 'name' => 'a'], - ] - ))->sortBy('id'); - $this->assertEquals(json_encode([ - 4 => ['id' => 1, 'name' => 'a'], - 3 => ['id' => 2, 'name' => 'b'], - 2 => ['id' => 3, 'name' => 'c'], - 1 => ['id' => 4, 'name' => 'd'], - 0 => ['id' => 5, 'name' => 'e'], - ]), (string) $data); - $this->assertEquals(json_encode([ - ['id' => 1, 'name' => 'a'], - ['id' => 2, 'name' => 'b'], - ['id' => 3, 'name' => 'c'], - ['id' => 4, 'name' => 'd'], - ['id' => 5, 'name' => 'e'], - ]), (string) $data->values()); - $dataMany = (new $collection( - [ - ['id' => 5, 'name' => 'e'], - ['id' => 4, 'name' => 'd'], - ['id' => 3, 'name' => 'c'], - ['id' => 2, 'name' => 'b'], - ['id' => 1, 'name' => 'a'], - ] - ))->sortBy(['id', 'asc']); - $this->assertEquals((string) $data, (string) $dataMany); - - $data = (new $collection( - [ - ['id' => 5, 'name' => '5a'], - ['id' => 4, 'name' => '4b'], - ['id' => 3, 'name' => 'c3'], - ['id' => 2, 'name' => '2d'], - ['id' => 1, 'name' => '1e'], - ] - ))->sortBy('name', SORT_NUMERIC); - $this->assertEquals(json_encode([ - 2 => ['id' => 3, 'name' => 'c3'], - 4 => ['id' => 1, 'name' => '1e'], - 3 => ['id' => 2, 'name' => '2d'], - 1 => ['id' => 4, 'name' => '4b'], - 0 => ['id' => 5, 'name' => '5a'], - ]), (string) $data); - $dataMany = (new $collection( - [ - ['id' => 5, 'name' => '5a'], - ['id' => 4, 'name' => '4b'], - ['id' => 3, 'name' => 'c3'], - ['id' => 2, 'name' => '2d'], - ['id' => 1, 'name' => '1e'], - ] - ))->sortBy([['name', 'asc']], SORT_NUMERIC); - $this->assertEquals((string) $data, (string) $dataMany); - - $data = (new $collection( - [ - ['id' => 5, 'name' => '5a'], - ['id' => 4, 'name' => '4b'], - ['id' => 3, 'name' => 'c3'], - ['id' => 2, 'name' => '2d'], - ['id' => 1, 'name' => '1e'], - ] - ))->sortBy('name', SORT_STRING); - $this->assertEquals(json_encode([ - 4 => ['id' => 1, 'name' => '1e'], - 3 => ['id' => 2, 'name' => '2d'], - 1 => ['id' => 4, 'name' => '4b'], - 0 => ['id' => 5, 'name' => '5a'], - 2 => ['id' => 3, 'name' => 'c3'], - ]), (string) $data); - $dataMany = (new $collection( - [ - ['id' => 5, 'name' => '5a'], - ['id' => 4, 'name' => '4b'], - ['id' => 3, 'name' => 'c3'], - ['id' => 2, 'name' => '2d'], - ['id' => 1, 'name' => '1e'], - ] - ))->sortBy([['name', 'asc']], SORT_STRING); - $this->assertEquals((string) $data, (string) $dataMany); - - $data = (new $collection( - [ - ['id' => 5, 'name' => 'a10'], - ['id' => 4, 'name' => 'a4'], - ['id' => 3, 'name' => 'a3'], - ['id' => 2, 'name' => 'a2'], - ['id' => 1, 'name' => 'a1'], - ] - ))->sortBy('name', SORT_NATURAL); - $this->assertEquals(json_encode([ - 4 => ['id' => 1, 'name' => 'a1'], - 3 => ['id' => 2, 'name' => 'a2'], - 2 => ['id' => 3, 'name' => 'a3'], - 1 => ['id' => 4, 'name' => 'a4'], - 0 => ['id' => 5, 'name' => 'a10'], - ]), (string) $data); - $dataMany = (new $collection( - [ - ['id' => 5, 'name' => 'a10'], - ['id' => 4, 'name' => 'a4'], - ['id' => 3, 'name' => 'a3'], - ['id' => 2, 'name' => 'a2'], - ['id' => 1, 'name' => 'a1'], - ] - ))->sortBy([['name', 'asc']], SORT_NATURAL); - $this->assertEquals((string) $data, (string) $dataMany); - - setlocale(LC_COLLATE, 'en_US.utf8'); - $data = (new $collection( - [ - ['id' => 5, 'name' => 'A'], - ['id' => 4, 'name' => 'a'], - ['id' => 3, 'name' => 'B'], - ['id' => 2, 'name' => 'b'], - ['id' => 1, 'name' => 'c'], - ] - ))->sortBy('name', SORT_LOCALE_STRING); - $this->assertEquals(json_encode([ - 1 => ['id' => 4, 'name' => 'a'], - 0 => ['id' => 5, 'name' => 'A'], - 3 => ['id' => 2, 'name' => 'b'], - 2 => ['id' => 3, 'name' => 'B'], - 4 => ['id' => 1, 'name' => 'c'], - ]), (string) $data); - $dataMany = (new $collection( - [ - ['id' => 5, 'name' => 'A'], - ['id' => 4, 'name' => 'a'], - ['id' => 3, 'name' => 'B'], - ['id' => 2, 'name' => 'b'], - ['id' => 1, 'name' => 'c'], - ] - ))->sortBy([['name', 'asc']], SORT_LOCALE_STRING); - $this->assertEquals((string) $data, (string) $dataMany); - - $data = (new $collection( - [ - ['id' => 1, 'name' => 'a'], - ['id' => 2, 'name' => 'b'], - ['id' => 3, 'name' => 'c'], - ['id' => 4, 'name' => 'd'], - ['id' => 5, 'name' => 'e'], - ] - ))->sortByDesc('id'); - $this->assertEquals(json_encode([ - 4 => ['id' => 5, 'name' => 'e'], - 3 => ['id' => 4, 'name' => 'd'], - 2 => ['id' => 3, 'name' => 'c'], - 1 => ['id' => 2, 'name' => 'b'], - 0 => ['id' => 1, 'name' => 'a'], - ]), (string) $data); - $this->assertEquals(json_encode([ - ['id' => 5, 'name' => 'e'], - ['id' => 4, 'name' => 'd'], - ['id' => 3, 'name' => 'c'], - ['id' => 2, 'name' => 'b'], - ['id' => 1, 'name' => 'a'], - ]), (string) $data->values()); - $dataMany = (new $collection( - [ - ['id' => 1, 'name' => 'a'], - ['id' => 2, 'name' => 'b'], - ['id' => 3, 'name' => 'c'], - ['id' => 4, 'name' => 'd'], - ['id' => 5, 'name' => 'e'], - ] - ))->sortByDesc(['id']); - $this->assertEquals((string) $data, (string) $dataMany); - } }