<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'array.sorting.php',
    1 => '对数组进行排序',
    2 => '对数组进行排序',
  ),
  'up' => 
  array (
    0 => 'book.array.php',
    1 => '数组',
  ),
  'prev' => 
  array (
    0 => 'array.constants.php',
    1 => '预定义常量',
  ),
  'next' => 
  array (
    0 => 'ref.array.php',
    1 => '数组 函数',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    '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">对数组进行排序</h1>

 <p class="para">
  PHP 有一些用来排序数组的函数，
  这个文档会把它们列出来。
 </p>
 <p class="para">
  主要区别有：
 </p>
 <p class="para">
  <ul class="simplelist">
   <li>
    有些函数基于 <span class="type"><a href="language.types.array.php" class="type array">array</a></span> 的键来排序，
    而其他的基于值来排序的：<code class="literal">$array[&#039;key&#039;] = &#039;value&#039;;</code>。
   </li>
   <li>
    排序之后键和值之间的关联关系是否能够保持，
    是指排序之后数组的键可能
    会被重置为数字型的（0,1,2 ...）。
   </li>
   <li>
    排序的顺序有：字母表顺序， 升序（由低到高），
    降序（由高到低），数字排序，自然排序，随机顺序或者用户自定义排序。
   </li>
   <li>
    注意：下列的所有排序函数都是直接作用于数组本身，
    而不是返回一个新的有序的数组。
   </li>
   <li>
    以下函数对于数组中相等的元素，会保留原有的排序。
    在 PHP 8.0.0 之前，它们排序后的顺序是未定义的（也即相等元素之间的顺序是不稳定的）。
   </li>
  </ul>
 </p>
 <p class="para">
  <table class="doctable table">
   <caption><strong>排序函数属性</strong></caption>
   
    <thead>
     <tr>
      <th>函数名称</th>
      <th>排序依据</th>
      <th>数组索引键保持</th>
      <th>排序的顺序</th>
      <th>相关函数</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>值</td>
       <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span> 键保持不变，<span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 键重新索引</td>
      <td>第一个数组或者由选项指定</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>值</td>
      <td>是</td>
      <td>升序</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>值</td>
      <td>是</td>
      <td>降序</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>键</td>
      <td>是</td>
      <td>降序</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>键</td>
      <td>是</td>
      <td>升序</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>值</td>
      <td>是</td>
      <td>自然排序，大小写不敏感</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>值</td>
      <td>是</td>
      <td>自然排序</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>值</td>
      <td>否</td>
      <td>降序</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>值</td>
      <td>否</td>
      <td>随机</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>值</td>
      <td>否</td>
      <td>升序</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>值</td>
      <td>是</td>
      <td>由用户定义</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>键</td>
      <td>是</td>
      <td>由用户定义</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>值</td>
      <td>否</td>
      <td>由用户定义</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); ?>