diff --git a/src/RandomIterableAggregate.php b/src/RandomIterableAggregate.php index 578049d..25c8b88 100644 --- a/src/RandomIterableAggregate.php +++ b/src/RandomIterableAggregate.php @@ -26,14 +26,15 @@ public function __construct(private readonly iterable $iterable, private readonl public function getIterator(): Generator { $cache = new CachingIteratorAggregate((new PackIterableAggregate($this->iterable))->getIterator()); - $shuffle = new ShuffleIteratorAggregate(iterator_count($cache), $this->seed); - foreach ($shuffle as $number) { - foreach ($cache as $key => $value) { - if ($key === $number) { - yield $value[0] => $value[1]; - } - } - } + $sort = new SortIterableAggregate( + (new UnpackIterableAggregate(new MultipleIterableAggregate([ + new ShuffleIteratorAggregate(iterator_count($cache), $this->seed), + $cache, + ]))), + static fn (array $a, array $b, int $ka, int $kb): int => $ka <=> $kb + ); + + yield from new UnpackIterableAggregate($sort); } }