<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'array.sorting.php',
    1 => 'Sorting Arrays',
    2 => 'Sorting Arrays',
  ),
  'up' => 
  array (
    0 => 'book.array.php',
    1 => 'Arrays',
  ),
  'prev' => 
  array (
    0 => 'array.constants.php',
    1 => 'Predefined Constants',
  ),
  'next' => 
  array (
    0 => 'ref.array.php',
    1 => 'Array Functions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/array/sorting.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="array.sorting" class="chapter">
 <h1 class="title">Sorting Arrays</h1>

 <p class="para">
  PHP has several functions that deal with sorting arrays, and this
  document exists to help sort it all out.
 </p>
 <p class="para">
  The main differences are:
 </p>
 <p class="para">
  <ul class="simplelist">
   <li>
    Some sort based on the <span class="type"><a href="language.types.array.php" class="type array">array</a></span> keys, whereas others by
    the values: <code class="literal">$array[&#039;key&#039;] = &#039;value&#039;;</code>
   </li>
   <li>
    Whether or not the correlation between the keys and values are
    maintained after the sort, which may mean the keys are reset
    numerically (0,1,2 ...)
   </li>
   <li>
    The order of the sort: alphabetical, ascending (low to high),
    descending (high to low), natural, random, or user defined
   </li>
   <li>
    Note: All of these sort functions act directly on the array
    variable itself, as opposed to returning a new sorted array
   </li>
   <li>
    If any of these sort functions evaluates two members as equal
    then they retain their original order.
    Prior to PHP 8.0.0, their order were undefined (the sorting was not stable).
   </li>
  </ul>
 </p>
 <p class="para">
  <table class="doctable table">
   <caption><strong>Sorting function attributes</strong></caption>
   
    <thead>
     <tr>
      <th>Function name</th>
      <th>Sorts by</th>
      <th>Maintains key association</th>
      <th>Order of sort</th>
      <th>Related functions</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td><span class="function"><a href="function.array-multisort.php" class="function">array_multisort()</a></span></td>
      <td>value</td>
      <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span> keys yes, <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> keys no</td>
      <td>first array or sort options</td>
      <td><span class="function"><a href="function.array-walk.php" class="function">array_walk()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.asort.php" class="function">asort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>ascending</td>
      <td><span class="function"><a href="function.arsort.php" class="function">arsort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.arsort.php" class="function">arsort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>descending</td>
      <td><span class="function"><a href="function.asort.php" class="function">asort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.krsort.php" class="function">krsort()</a></span></td>
      <td>key</td>
      <td>yes</td>
      <td>descending</td>
      <td><span class="function"><a href="function.ksort.php" class="function">ksort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.ksort.php" class="function">ksort()</a></span></td>
      <td>key</td>
      <td>yes</td>
      <td>ascending</td>
      <td><span class="function"><a href="function.krsort.php" class="function">krsort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.natcasesort.php" class="function">natcasesort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>natural, case insensitive</td>
      <td><span class="function"><a href="function.natsort.php" class="function">natsort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.natsort.php" class="function">natsort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>natural</td>
      <td><span class="function"><a href="function.natcasesort.php" class="function">natcasesort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.rsort.php" class="function">rsort()</a></span></td>
      <td>value</td>
      <td>no</td>
      <td>descending</td>
      <td><span class="function"><a href="function.sort.php" class="function">sort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.shuffle.php" class="function">shuffle()</a></span></td>
      <td>value</td>
      <td>no</td>
      <td>random</td>
      <td><span class="function"><a href="function.array-rand.php" class="function">array_rand()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.sort.php" class="function">sort()</a></span></td>
      <td>value</td>
      <td>no</td>
      <td>ascending</td>
      <td><span class="function"><a href="function.rsort.php" class="function">rsort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.uasort.php" class="function">uasort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>user defined</td>
      <td><span class="function"><a href="function.uksort.php" class="function">uksort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.uksort.php" class="function">uksort()</a></span></td>
      <td>key</td>
      <td>yes</td>
      <td>user defined</td>
      <td><span class="function"><a href="function.uasort.php" class="function">uasort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.usort.php" class="function">usort()</a></span></td>
      <td>value</td>
      <td>no</td>
      <td>user defined</td>
      <td><span class="function"><a href="function.uasort.php" class="function">uasort()</a></span></td>
     </tr>

    </tbody>
   
  </table>

 </p>
</div>
<?php manual_footer($setup); ?>