<?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> <chapter xml:id="array.sorting" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> <title>Sorting Arrays</title> <para> PHP has several functions that deal with sorting arrays, and this document exists to help sort it all out. </para> <para> The main differences are: </para> <para> <simplelist> <member> Some sort based on the <type>array</type> keys, whereas others by the values: <literal>$array['key'] = 'value';</literal> </member> <member> 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 ...) </member> <member> The order of the sort: alphabetical, low to high (ascending), high to low (descending), numerical, natural, random, or user defined </member> <member> Note: All of these sort functions act directly on the array variable itself, as opposed to returning a new sorted array </member> <member> If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable). </member> </simplelist> </para> <para> <table> <title>Sorting function attributes</title> <tgroup cols="5"> <thead> <row> <entry>Function name</entry> <entry>Sorts by</entry> <entry>Maintains key association</entry> <entry>Order of sort</entry> <entry>Related functions</entry> </row> </thead> <tbody> <row> <entry><function>array_multisort</function></entry> <entry>value</entry> <entry>associative yes, numeric no</entry> <entry>first array or sort options</entry> <entry><function>array_walk</function></entry> </row> <row> <entry><function>asort</function></entry> <entry>value</entry> <entry>yes</entry> <entry>low to high</entry> <entry><function>arsort</function></entry> </row> <row> <entry><function>arsort</function></entry> <entry>value</entry> <entry>yes</entry> <entry>high to low</entry> <entry><function>asort</function></entry> </row> <row> <entry><function>krsort</function></entry> <entry>key</entry> <entry>yes</entry> <entry>high to low</entry> <entry><function>ksort</function></entry> </row> <row> <entry><function>ksort</function></entry> <entry>key</entry> <entry>yes</entry> <entry>low to high</entry> <entry><function>asort</function></entry> </row> <row> <entry><function>natcasesort</function></entry> <entry>value</entry> <entry>yes</entry> <entry>natural, case insensitive</entry> <entry><function>natsort</function></entry> </row> <row> <entry><function>natsort</function></entry> <entry>value</entry> <entry>yes</entry> <entry>natural</entry> <entry><function>natcasesort</function></entry> </row> <row> <entry><function>rsort</function></entry> <entry>value</entry> <entry>no</entry> <entry>high to low</entry> <entry><function>sort</function></entry> </row> <row> <entry><function>shuffle</function></entry> <entry>value</entry> <entry>no</entry> <entry>random</entry> <entry><function>array_rand</function></entry> </row> <row> <entry><function>sort</function></entry> <entry>value</entry> <entry>no</entry> <entry>low to high</entry> <entry><function>rsort</function></entry> </row> <row> <entry><function>uasort</function></entry> <entry>value</entry> <entry>yes</entry> <entry>user defined</entry> <entry><function>uksort</function></entry> </row> <row> <entry><function>uksort</function></entry> <entry>key</entry> <entry>yes</entry> <entry>user defined</entry> <entry><function>uasort</function></entry> </row> <row> <entry><function>usort</function></entry> <entry>value</entry> <entry>no</entry> <entry>user defined</entry> <entry><function>uasort</function></entry> </row> </tbody> </tgroup> </table> </para> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"~/.phpdoc/manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->