Quoted from here:
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.
In my opinion, cmp_function needs only to return 1 if the first is greater than the second.
Why does it need all 3 cases ?
UPDATE
function bubble($list)
{
$length = count($list) - 2;
$sorted = false;
while(!$sorted)
{
$sorted = true;
foreach(range(0,$length) as $i)
{
if($list[$i] > $list[$i + 1])
{
$sorted = false;
list($list[$i],$list[$i + 1]) = array($list[$i + 1],$list[$i]);
}
}
}
return $list;
}
So that it can distinguish between
<,==and>.This is similar to the requirements on the callback for e.g. C’s
qsort(for all I know,usortmay be implemented usingqsortinternally).It is possible to write a search routine that only needs a two-valued predicate, but
usorthappens not to be one of them.