Skip to content

Commit 19c9b80

Browse files
committed
Sort the results of PriorityQueue::toArray() according to priority
Closes laminas#21 Signed-off-by: George Steel <[email protected]>
1 parent e70f8e1 commit 19c9b80

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/PriorityQueue.php

+16-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use function serialize;
1818
use function sprintf;
1919
use function unserialize;
20+
use function usort;
2021

2122
/**
2223
* Re-usable, serializable priority queue implementation
@@ -245,13 +246,12 @@ public function __unserialize(array $data): void
245246
}
246247

247248
/**
248-
* Serialize to an array
249-
* By default, returns only the item data, and in the order registered (not
250-
* sorted). You may provide one of the EXTR_* flags as an argument, allowing
249+
* Serialize to an array, ordered by priority
250+
* By default, returns only the item data. You may provide one of the EXTR_* flags as an argument, allowing
251251
* the ability to return priorities or both data and priority.
252252
*
253253
* @param self::EXTR_* $flag
254-
* @return array<array-key, mixed>
254+
* @return list<mixed>
255255
* @psalm-return ($flag is self::EXTR_BOTH
256256
* ? list<array{data: TValue, priority: int}>
257257
* : $flag is self::EXTR_PRIORITY
@@ -261,10 +261,19 @@ public function __unserialize(array $data): void
261261
*/
262262
public function toArray(int $flag = self::EXTR_DATA): array
263263
{
264+
/**
265+
* A manual sort is required because $item['priority'] as stored in the inner queue could be an array as
266+
* opposed to an integer.
267+
*/
268+
$items = $this->items;
269+
usort($items, function (array $a, array $b): int {
270+
return $b['priority'] <=> $a['priority'];
271+
});
272+
264273
return match ($flag) {
265-
self::EXTR_BOTH => $this->items,
266-
self::EXTR_PRIORITY => array_map(static fn($item): int => $item['priority'], $this->items),
267-
default => array_map(static fn($item): mixed => $item['data'], $this->items),
274+
self::EXTR_BOTH => $items,
275+
self::EXTR_PRIORITY => array_map(static fn($item): int => $item['priority'], $items),
276+
default => array_map(static fn($item): mixed => $item['data'], $items),
268277
};
269278
}
270279

test/PriorityQueueTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public function testSerializationAndDeserializationShouldMaintainState(): void
5656
public function testRetrievingQueueAsArrayReturnsDataOnlyByDefault(): void
5757
{
5858
$expected = [
59-
'foo',
6059
'bar',
60+
'foo',
6161
'baz',
6262
'bat',
6363
];
@@ -68,8 +68,8 @@ public function testRetrievingQueueAsArrayReturnsDataOnlyByDefault(): void
6868
public function testCanCastToArrayOfPrioritiesOnly(): void
6969
{
7070
$expected = [
71-
3,
7271
4,
72+
3,
7373
2,
7474
1,
7575
];
@@ -80,8 +80,8 @@ public function testCanCastToArrayOfPrioritiesOnly(): void
8080
public function testCanCastToArrayOfDataPriorityPairs(): void
8181
{
8282
$expected = [
83-
['data' => 'foo', 'priority' => 3],
8483
['data' => 'bar', 'priority' => 4],
84+
['data' => 'foo', 'priority' => 3],
8585
['data' => 'baz', 'priority' => 2],
8686
['data' => 'bat', 'priority' => 1],
8787
];

0 commit comments

Comments
 (0)