the usort "s puzzle

the usort "s puzzle

am 30.09.2007 05:37:16 von youngord

$mix=array(
array("A",10),
array("B",5),
array("C",100)
);
function com($x,$y){
echo $x[0];
}
usort($mix,'com');


?>

i think the $x[0] result is A,
but the final $x[0] result is BC.
why???

Re: the usort "s puzzle

am 30.09.2007 06:18:04 von Michael Fesser

..oO(youngord@gmail.com)

> >$mix=array(
> array("A",10),
> array("B",5),
> array("C",100)
> );
>function com($x,$y){
>echo $x[0];
>}
>usort($mix,'com');
>
>
>?>
>
>i think the $x[0] result is A,
>but the final $x[0] result is BC.
>why???

What are you trying to do? The above doesn't really makes sense.
The result depends on what sorting algorithm PHP uses internally.

Micha

Re: the usort "s puzzle

am 30.09.2007 12:30:56 von Shelly

wrote in message
news:1191123436.050247.225700@g4g2000hsf.googlegroups.com...
> > $mix=array(
> array("A",10),
> array("B",5),
> array("C",100)
> );
> function com($x,$y){
> echo $x[0];
> }
> usort($mix,'com');
>
>
> ?>
>
> i think the $x[0] result is A,
> but the final $x[0] result is BC.
> why???
>

From www.php.net:
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.

So, what does "com" return?

Shelly

Re: the usort "s puzzle

am 30.09.2007 14:46:14 von luiheidsgoeroe

On Sun, 30 Sep 2007 05:37:16 +0200, wrote:

> > $mix=3Darray(
> array("A",10),
> array("B",5),
> array("C",100)
> );
> function com($x,$y){
> echo $x[0];
> }
> usort($mix,'com');
>
>
> ?>
>
> i think the $x[0] result is A,
> but the final $x[0] result is BC.
> why???

Because PHP uses an internal construct to sort, starting whereever it's =
=

internal functionality is written to start, which should not matter. If =
it =

compares (B,A) & (C,B), it knows enough to sort.
-- =

Rik Wasmus