diff --git a/src/Collection/GenericList.php b/src/Collection/GenericList.php index 7229259..a70896f 100644 --- a/src/Collection/GenericList.php +++ b/src/Collection/GenericList.php @@ -103,6 +103,14 @@ public function filter(callable $predicate) return $filtered->reverse(); } + /** + * @return GenericList + */ + public function sorted() + { + return self::ofAll($this->iterator()->sort()); + } + /** * @param callable(T):bool $predicate * diff --git a/src/Collection/Iterator.php b/src/Collection/Iterator.php index 2b03633..e8c457b 100644 --- a/src/Collection/Iterator.php +++ b/src/Collection/Iterator.php @@ -165,4 +165,12 @@ public function fold($zero, callable $combine) return $zero; } + + public function sort(): self + { + $array = $this->toArray(); + asort($array); + + return self::of(...$array); + } } diff --git a/src/Collection/Map.php b/src/Collection/Map.php index 826b253..d421ad3 100644 --- a/src/Collection/Map.php +++ b/src/Collection/Map.php @@ -203,6 +203,17 @@ public function filter(callable $predicate) return self::fromPointer($map); } + /** + * @return Map + */ + public function sorted() + { + $map = $this->map; + asort($map); + + return self::fromPointer($map); + } + /** * @param callable(Tuple):bool $predicate * diff --git a/src/Collection/Set.php b/src/Collection/Set.php index ce531a8..63f456d 100644 --- a/src/Collection/Set.php +++ b/src/Collection/Set.php @@ -216,6 +216,17 @@ public function filter(callable $predicate) return self::fromPointer($filtered); } + /** + * @return Set + */ + public function sorted() + { + $elements = $this->elements; + asort($elements); + + return self::fromPointer($elements); + } + /** * @param callable(T):bool $predicate * diff --git a/src/Collection/Stream.php b/src/Collection/Stream.php index baec65c..af92774 100644 --- a/src/Collection/Stream.php +++ b/src/Collection/Stream.php @@ -198,6 +198,14 @@ public function filter(callable $predicate) }); } + /** + * @return Stream + */ + public function sorted() + { + return self::ofAll($this->getIterator()->sort()); + } + /** * @param callable(T):bool $predicate * diff --git a/src/Collection/Traversable.php b/src/Collection/Traversable.php index 69a878e..cd3797b 100644 --- a/src/Collection/Traversable.php +++ b/src/Collection/Traversable.php @@ -56,6 +56,11 @@ abstract public function map(callable $mapper); */ abstract public function filter(callable $predicate); + /** + * @return Traversable + */ + abstract public function sorted(); + /** * @return Traversable */ diff --git a/tests/Collection/GenericListTest.php b/tests/Collection/GenericListTest.php index 771a13a..453446a 100644 --- a/tests/Collection/GenericListTest.php +++ b/tests/Collection/GenericListTest.php @@ -203,4 +203,9 @@ public function testListAppendAll(): void self::assertTrue(GenericList::of(1, 2, 3, 4)->equals(GenericList::of(1, 2)->appendAll(GenericList::of(3, 4)))); self::assertTrue(GenericList::of('a', 'b', 'c', 'd', 'e')->equals(GenericList::of('a')->appendAll(GenericList::of('b', 'c', 'd', 'e')))); } + + public function testListSorted(): void + { + self::assertTrue(GenericList::of('a', 'b', 'c', 'd', 'e')->equals(GenericList::of('e', 'd', 'c', 'b', 'a')->sorted())); + } } diff --git a/tests/Collection/MapTest.php b/tests/Collection/MapTest.php index d5c1924..e9280e8 100644 --- a/tests/Collection/MapTest.php +++ b/tests/Collection/MapTest.php @@ -278,4 +278,9 @@ public function testMapToArray(): void { self::assertEquals(['a' => 'b', 'c' => 'd'], Map::fromArray(['a' => 'b', 'c' => 'd'])->toArray()); } + + public function testMapSorted(): void + { + self::assertTrue(Map::fromArray(['a' => 'b', 'c' => 'd', 'e' => 'f'])->equals(Map::fromArray(['e' => 'f', 'c' => 'd', 'a' => 'b'])->sorted())); + } } diff --git a/tests/Collection/SetTest.php b/tests/Collection/SetTest.php index cd22699..f61af23 100644 --- a/tests/Collection/SetTest.php +++ b/tests/Collection/SetTest.php @@ -227,4 +227,9 @@ public function testToArray(): void { self::assertEquals(['php', 'is', 'awesome'], Set::of('php', 'is', 'awesome')->toArray()); } + + public function testSorted(): void + { + self::assertTrue(Set::of('a', 'b', 'c', 'd', 'e')->equals(Set::of('e', 'd', 'c', 'b', 'a')->sorted())); + } } diff --git a/tests/Collection/StreamTest.php b/tests/Collection/StreamTest.php index dd47393..5519866 100644 --- a/tests/Collection/StreamTest.php +++ b/tests/Collection/StreamTest.php @@ -252,4 +252,9 @@ public function testStreamToArray(): void { self::assertEquals([1, 2, 3, 4, 5], Stream::range(1, 5)->toArray()); } + + public function testStreamSorted(): void + { + self::assertTrue(Stream::of('a', 'b', 'c', 'd', 'e')->equals(Stream::of('e', 'd', 'c', 'b', 'a')->sorted())); + } }