php - How SplPriorityQueue works when priority is not an integer? -


i wondering how splpriorityqueue works when priority string or int. quick example:

    $queue = new \splpriorityqueue();      $queue->insert('b', 5);     $queue->insert('c', 5);     $queue->insert('d', 1);     $queue->insert('a', 10);     $queue->insert('1', 'a');     $queue->insert('2', 'b');      print_r($queue); 

output:

array (     [5] =>     [4] => b     [3] => c     [2] => d     [1] => 2     [0] => 1 ) 

question: why items int priority listed first (i.e. b c d)? when priority string (items 1 2), b considered greater a?

this determined splpriorityqueue::compare(). documentation states return value:

result of comparison, positive integer if priority1 greater priority2, 0 if equal, negative integer otherwise.

note:

multiple elements same priority dequeued in no particular order.

note, parameters priority1 , priority2 declared mixed , there no mention of converting int.

this means, usual rules > apply (see comparison operator documentation):

  • string compared string: lexical comparison (numerical if both strings numerical)
  • int compared int: numerical comparison
  • string compared int: string converted number, numerical comparison

(int)'a' , (int)'b' resolves 0, why these items come last after numbers.

these relevant comparisons example:

php > var_dump(1 > 'a'); bool(true) php > var_dump(1 > 'b'); bool(true) php > var_dump('b' > 'a'); bool(true) 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -