title: How the usort() sorting algorithm works?
body: <p>I have an usort() example and I added some echo statements to see how the code works: </p>
  
  <pre><code>&lt;?php
  function list_cmp($a, $b) {
      global $order;
      echo "\$a=$a, \$b=$b &lt;/br&gt;";
  
      foreach ($order as $key =&gt; $value) {
          echo "\$value=$value &lt;/br&gt;";
          if ($a == $value) {
              echo "\$a=\$value, returing 0. &lt;/br&gt;";
              return 0;
          }
          if ($b == $value) {
              echo "\$b=\$value, returing 1. &lt;/br&gt;";
              return 1;
          }
      }
  }
  
  $order[0] = 1;
  $order[1] = 3;
  $order[2] = 4;
  $order[3] = 2;
  
  $array[0] = 2;
  $array[1] = 1;
  $array[2] = 3;
  $array[3] = 4;
  $array[4] = 2;
  array[5] = 1;
  $array[6] = 2;
  
  usort($array, "list_cmp");
  ?&gt;
  </code></pre>
  
  <p>The output of the code is this: </p>
  
  <pre><code>$a=2, $b=1 
  $value=1 
  $b=$value, returing 1. 
  $a=2, $b=3 
  $value=1 
  $value=3 
  $b=$value, returing 1. 
  $a=1, $b=3 
  $value=1 
  $a=$value, returing 0. 
  $a=2, $b=4 
  $value=1 
  $value=3 
  $value=4 
  $b=$value, returing 1. 
  $a=3, $b=4 
  $value=1 
  $value=3 
  $a=$value, returing 0. 
  $a=2, $b=2 
  $value=1 
  $value=3 
  $value=4 
  $value=2 
  $a=$value, returing 0. 
  $a=2, $b=1 
  $value=1 
  $b=$value, returing 1. 
  $a=2, $b=1 
  $value=1 
  $b=$value, returing 1. 
  $a=4, $b=1 
  $value=1 
  $b=$value, returing 1. 
  $a=3, $b=1 
  $value=1 
  $b=$value, returing 1. 
  $a=1, $b=1 
  $value=1 
  $a=$value, returing 0. 
  $a=2, $b=2 
  $value=1 
  $value=3 
  $value=4 
  $value=2 
  $a=$value, returing 0. 
  </code></pre>
  
  <p>What is the mechanism of creating the 12 $a-$b pairs - 2-1, 2-3, 1-3, 2-4, 3-4, 2-2, 2-1, 2-1 (the same again?), 4-1, 3-1, 1-1, 2-2. The above code returns 1,1,0,1,0,0,1,1,1,1,0,0. And also what is the mechanism of sorting the array, based on the values that get returned? I am trying to understand how the usort() mechanism works. </p>
  
  <p>Thanks. </p>