<?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 => 'ja',
  ),
  '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' => 'ja',
    '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>
    配列のキーでソートするものと、値でソートするものがあります。
    <code class="literal">$array[&#039;キー&#039;] = &#039;値&#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>文字列がキーの場合は維持し、数値添字配列の場合は維持しない</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); ?>