|
Posted by Paul Lautman on 05/21/06 16:08
Janwillem Borleffs wrote:
> Paul Lautman wrote:
>> Can you give me an example of where usort and uasort give different
>> outputs and how uksort would be used?
>>
>
> function cmp($a, $b) {
> if ($a == $b) return 0;
> return $a < $b ? -1 : 1;
> }
>
> $array = array('b' => 200, 'a' => 50, 'A' => 100);
> $usort = $uasort = $uksort = $array;
>
> print 'original array:<br>';
> print_r($array);
>
> print '<hr>usort: sorted by value, keys are not preserved:<br>';
> usort($usort, 'cmp');
> print_r($usort);
>
> print '<hr>uasort: sorted by value, keys are preserved:<br>';
> uasort($uasort, 'cmp');
> print_r($uasort);
>
> print '<hr>uksort: sorted by key;<br>';
> print '"A" put before "a" because of its lower ASCII value:<br>';
> uksort($uksort, 'cmp');
> print_r($uksort);
>
>
> JW
Cheers for that, do you also know why I see the oder reversal (see the first
post)?
[Back to original message]
|