Re: sorting in array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



For starters, the function is supposed to return 0 for countries that
are equal.

As it stands now, it's going to return 1 / -1 randomly based on which
arg happens to be $a versus $b.

That's bad.

For some implementations of shuffle/sort routines, it will actually
crash.

I forget which implementation does this, but it's got to do with
caching the comparison result in a B-tree and then walking the tree
assuming that the comparison will be consistent...

Anyway, as far as "doesn't work" goes, you'd have to give us more info
about how it's not working...

On Mon, July 31, 2006 1:56 am, weetat wrote:
>
> Hi ,
>
>    Doesn't work .
>    Any ideas ?
>
> Thanks
> Peter Lauri wrote:
>>    function cmpcountry($a, $b)
>>    {
>>
>>          $country1 = $a['country'];
>>          $country2 = $b['country'];
>>
>> 	   if($country1=='') return 1;
>> 	   else return ($country1 < $country2) ? -1 : 1;
>>
>>    }
>>
>> -----Original Message-----
>> From: weetat [mailto:weetat.yeo@xxxxxxxxx]
>> Sent: Monday, July 31, 2006 12:32 PM
>> To: php-general@xxxxxxxxxxxxx
>> Subject:  sorting in array
>>
>> Hi all ,
>>
>>   I have array value as shown below, i have paste my test php code
>> below:
>>   I have problem when doing usort() when 'country' = '', i would
>> like to
>> display records where country = '' last. Any ideas how to do that ?
>>
>> Thanks
>>
>>    $arraytest= array(
>>       array
>>          (
>>              'country' => '',
>>          )
>>       ,
>>       array
>>          (
>>              'country' => 'Thailand',
>>          )
>>       ,
>>       array
>>          (
>>              'country' => 'Singapore',
>>          )
>>       ,
>>       array
>>          (
>>              'country' => 'Singapore',
>>          )
>>       ,
>>       array
>>          (
>>              'country' => '',
>>          )
>>        ,
>> 	  array
>>          (
>>              'country' => '',
>>          )
>> 	,
>> 	array
>>          (
>>              'country' => '',
>>          )
>>
>>       );
>>
>>
>>    function cmpcountry($a, $b)
>>    {
>>
>>          $country1 = $a['country'];
>>          $country2 = $b['country'];
>>
>>           return ($country1 < $country2) ? -1 : 1;
>>    }
>>
>>      usort($arraytest,"cmpcountry");
>>      while(list($name,$value)=each($arraytest)){
>> 	   echo $name."<br><br>";
>>
>> 	   while(list($n,$v) = each($arraytest[$name])){
>> 	       echo $v."<br><br>";
>> 	   }
>>       }
>>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux