usort Sort an array by values using a user-defined comparison function &reftitle.description; boolusort arrayarray callbackcmp_function This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function. If two members compare as equal, their order in the sorted array is undefined. ¬e.no-key-association; &reftitle.parameters; array The input array. cmp_function 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. &reftitle.returnvalues; &return.success; &reftitle.changelog; &Version; &Description; 4.1.0 A new sort algorithm was introduced. The cmp_function doesn't keep the original order for elements comparing as equal. &reftitle.examples; <function>usort</function> example $value) { echo "$key: $value\n"; } ?> ]]> &example.outputs; Obviously in this trivial case the sort function would be more appropriate. <function>usort</function> example using multi-dimensional array ]]> When sorting a multi-dimensional array, $a and $b contain references to the first index of the array. &example.outputs; <function>usort</function> example using a member function of an object name = $name; } /* This is the static comparing function: */ static function cmp_obj($a, $b) { $al = strtolower($a->name); $bl = strtolower($b->name); if ($al == $bl) { return 0; } return ($al > $bl) ? +1 : -1; } } $a[] = new TestObj("c"); $a[] = new TestObj("b"); $a[] = new TestObj("d"); usort($a, array("TestObj", "cmp_obj")); foreach ($a as $item) { echo $item->name . "\n"; } ?> ]]> &example.outputs; &reftitle.seealso; uasort uksort sort asort arsort ksort natsort rsort