<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.arrayobject.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'arrayobject.uasort.php',
    1 => 'ArrayObject::uasort',
    2 => 'Sort the entries with a user-defined comparison function and maintain key association',
  ),
  'up' => 
  array (
    0 => 'class.arrayobject.php',
    1 => 'ArrayObject',
  ),
  'prev' => 
  array (
    0 => 'arrayobject.setiteratorclass.php',
    1 => 'ArrayObject::setIteratorClass',
  ),
  'next' => 
  array (
    0 => 'arrayobject.uksort.php',
    1 => 'ArrayObject::uksort',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/spl/arrayobject/uasort.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="arrayobject.uasort" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ArrayObject::uasort</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">ArrayObject::uasort</span> &mdash; <span class="dc-title">Sort the entries with a user-defined comparison function and maintain key association</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-arrayobject.uasort-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>ArrayObject::uasort</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>): <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span></div>

  <p class="para rdfs-comment">
   This function sorts the entries such that keys maintain their
   correlation with the entry that they are associated with, using a
   user-defined comparison function.
  </p>
  <p class="para">
   This is used mainly when sorting associative arrays where the actual
   element order is significant.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
 <p class="para">
  If two members compare as equal, they retain their original order.
  Prior to PHP 8.0.0, their relative order in the sorted array was undefined.
 </p>
</p></blockquote>

 </div>


 <div class="refsect1 parameters" id="refsect1-arrayobject.uasort-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">callback</code></dt>
     <dd>
      <p class="para">
 The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
</p>
<div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">callback</span></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$a</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$b</code></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

<div class="caution"><strong class="caution">Caution</strong>
 <p class="para">
  Returning <em>non-integer</em> values from the comparison
  function, such as <span class="type"><a href="language.types.float.php" class="type float">float</a></span>, will result in an internal cast to
  <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> of the callback&#039;s return value. So values such as
  <code class="literal">0.99</code> and <code class="literal">0.1</code> will both be cast to an
  integer value of <code class="literal">0</code>, which will compare such values as equal.
 </p>
</div>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-arrayobject.uasort-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Always returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-arrayobject.uasort-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
 <td>8.2.0</td>
 <td>
  The return type is <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> now; previously, it was <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>.
 </td>
</tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-arrayobject.uasort-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>ArrayObject::uasort()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Comparison function<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">cmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {<br />    if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {<br />        return </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />    }<br />    return (</span><span style="color: #0000BB">$a </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">$b</span><span style="color: #007700">) ? -</span><span style="color: #0000BB">1 </span><span style="color: #007700">: </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Array to be sorted<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'a' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #DD0000">'b' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">8</span><span style="color: #007700">, </span><span style="color: #DD0000">'c' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'d' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">9</span><span style="color: #007700">, </span><span style="color: #DD0000">'e' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'f' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #DD0000">'g' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">'h' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$arrayObject </span><span style="color: #007700">= new </span><span style="color: #0000BB">ArrayObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arrayObject</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Sort and print the resulting array<br /></span><span style="color: #0000BB">$arrayObject</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">uasort</span><span style="color: #007700">(</span><span style="color: #DD0000">'cmp'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arrayObject</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">object(ArrayObject)#1 (1) {
  [&quot;storage&quot;:&quot;ArrayObject&quot;:private]=&gt;
  array(8) {
    [&quot;a&quot;]=&gt;
    int(4)
    [&quot;b&quot;]=&gt;
    int(8)
    [&quot;c&quot;]=&gt;
    int(-1)
    [&quot;d&quot;]=&gt;
    int(-9)
    [&quot;e&quot;]=&gt;
    int(2)
    [&quot;f&quot;]=&gt;
    int(5)
    [&quot;g&quot;]=&gt;
    int(3)
    [&quot;h&quot;]=&gt;
    int(-4)
  }
}
object(ArrayObject)#1 (1) {
  [&quot;storage&quot;:&quot;ArrayObject&quot;:private]=&gt;
  array(8) {
    [&quot;d&quot;]=&gt;
    int(-9)
    [&quot;h&quot;]=&gt;
    int(-4)
    [&quot;c&quot;]=&gt;
    int(-1)
    [&quot;e&quot;]=&gt;
    int(2)
    [&quot;g&quot;]=&gt;
    int(3)
    [&quot;a&quot;]=&gt;
    int(4)
    [&quot;f&quot;]=&gt;
    int(5)
    [&quot;b&quot;]=&gt;
    int(8)
  }
}</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-arrayobject.uasort-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="arrayobject.asort.php" class="methodname" rel="rdfs-seeAlso">ArrayObject::asort()</a> - Sort the entries by value</span></li>
    <li><span class="methodname"><a href="arrayobject.ksort.php" class="methodname" rel="rdfs-seeAlso">ArrayObject::ksort()</a> - Sort the entries by key</span></li>
    <li><span class="methodname"><a href="arrayobject.natsort.php" class="methodname" rel="rdfs-seeAlso">ArrayObject::natsort()</a> - Sort entries using a &quot;natural order&quot; algorithm</span></li>
    <li><span class="methodname"><a href="arrayobject.natcasesort.php" class="methodname" rel="rdfs-seeAlso">ArrayObject::natcasesort()</a> - Sort an array using a case insensitive &quot;natural order&quot; algorithm</span></li>
    <li><span class="methodname"><a href="arrayobject.uksort.php" class="methodname" rel="rdfs-seeAlso">ArrayObject::uksort()</a> - Sort the entries by keys using a user-defined comparison function</span></li>
    <li><span class="function"><a href="function.uasort.php" class="function" rel="rdfs-seeAlso">uasort()</a> - Sort an array with a user-defined comparison function and maintain index association</span></li>
   </ul>
  </p>
 </div>


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