1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 290982 $ -->
<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
-->
|