Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The sorting algorithms used in PHP have this property where the order is undefined if the items match.</p> <p>If you need to keep the order, then you have to roll your own code.</p> <p>Fortunately some one already has: <a href="http://www.php.net/manual/en/function.usort.php#38827" rel="nofollow">http://www.php.net/manual/en/function.usort.php#38827</a></p> <pre><code>$data = array( array('name' =&gt; 'Albert', 'last' =&gt; 'Einstein'), array('name' =&gt; 'Lieserl', 'last' =&gt; 'Einstein'), array('name' =&gt; 'Alan', 'last' =&gt; 'Turing' ), array('name' =&gt; 'Mileva', 'last' =&gt; 'Einstein'), array('name' =&gt; 'Hans Albert', 'last' =&gt; 'Einstein') ); function sort_some_people($a, $b) { return strcmp($a['last'], $b['last']); } function mergesort(&amp;$array, $cmp_function = 'strcmp') { // Arrays of size &lt; 2 require no action. if (count($array) &lt; 2) return; // Split the array in half $halfway = count($array) / 2; $array1 = array_slice($array, 0, $halfway); $array2 = array_slice($array, $halfway); // Recurse to sort the two halves mergesort($array1, $cmp_function); mergesort($array2, $cmp_function); // If all of $array1 is &lt;= all of $array2, just append them. if (call_user_func($cmp_function, end($array1), $array2[0]) &lt; 1) { $array = array_merge($array1, $array2); return; } // Merge the two sorted arrays into a single sorted array $array = array(); $ptr1 = $ptr2 = 0; while ($ptr1 &lt; count($array1) &amp;&amp; $ptr2 &lt; count($array2)) { if (call_user_func($cmp_function, $array1[$ptr1], $array2[$ptr2]) &lt; 1) { $array[] = $array1[$ptr1++]; } else { $array[] = $array2[$ptr2++]; } } // Merge the remainder while ($ptr1 &lt; count($array1)) $array[] = $array1[$ptr1++]; while ($ptr2 &lt; count($array2)) $array[] = $array2[$ptr2++]; return; } mergesort($data, 'sort_some_people'); print_r($data); </code></pre> <p>Output:</p> <pre><code>Array ( [0] =&gt; Array ( [name] =&gt; Albert [last] =&gt; Einstein ) [1] =&gt; Array ( [name] =&gt; Lieserl [last] =&gt; Einstein ) [2] =&gt; Array ( [name] =&gt; Mileva [last] =&gt; Einstein ) [3] =&gt; Array ( [name] =&gt; Hans Albert [last] =&gt; Einstein ) [4] =&gt; Array ( [name] =&gt; Alan [last] =&gt; Turing ) ) </code></pre> <p>Voila! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload