Question

This question is related to this one I asked some time ago.

Basically it's about how dequeuing works in some borderline cases. For example:

$queue = new SplPriorityQueue();

$queue->insert('foo', 0);
$queue->insert('bar', 0);
$queue->insert('baz', '0');

I was expecting exactly the same order foo bar baz:

while ($queue->valid()) {
    var_dump($queue->current());
    $queue->next();
}

Turns out that is foo baz bar. Can you explain why baz goes before bar?

Was it helpful?

Solution

As foo, bar & baz all have the same priority their order is undefined, so they could come out in any order. See the note here http://www.php.net/manual/en/splpriorityqueue.compare.php

Note:

Multiple elements with the same priority will get dequeued in no particular order.

and the top user note on the same page for a suggested solution http://www.php.net/manual/en/splpriorityqueue.compare.php#93999

There is also a blog post on Matthew Weier O'Phinney's Blog which you may find useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top