Re: [PHP] sorting in array
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:[EMAIL PROTECTED] >> Sent: Monday, July 31, 2006 12:32 PM >> To: php-general@lists.php.net >> Subject: [PHP] 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.""; >> >> while(list($n,$v) = each($arraytest[$name])){ >> echo $v.""; >> } >> } >> > > -- > 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
Re: [PHP] sorting in array
weetat wrote: > 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 ? > ... You might try searching the list's archive's. This question has been asked and answered several times before. http://aspn.activestate.com/ASPN/Mail/Message/php-general/3172783 http://aspn.activestate.com/ASPN/Mail/Message/php-general/3199536 http://aspn.activestate.com/ASPN/Mail/Message/php-general/3212898 one of the solutions offered: http://aspn.activestate.com/ASPN/Mail/Message/3172873 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting in array
Sorry, I just woke up and just posted an reply without thinking, use this: function cmpcountry($a, $b) { $country1 = $a['country']; $country2 = $b['country']; if($country1=='' OR $country2=='') { if($country1==$country2) return 0; elseif($country1=='') return 1; else return -1; } else return ($country1 < $country2) ? -1 : 1; } /Peter -Original Message- From: Paul Novitski [mailto:[EMAIL PROTECTED] Sent: Monday, July 31, 2006 1:57 PM To: php-general@lists.php.net Subject: Re: [PHP] sorting in array At 10:31 PM 7/30/2006, weetat wrote: > I have problem when doing usort() when 'country' = '', i would > like to display records where country = '' last. Any ideas how to do that ? ... > $arraytest= array( > array > ( > 'country' => '', > ) > , > array > ( > 'country' => 'Thailand', > ) ... > function cmpcountry($a, $b) > { > $country1 = $a['country']; > $country2 = $b['country']; > > return ($country1 < $country2) ? -1 : 1; > } Weetat, You can sort the blank fields to the end simply by forcing their evaluated values in your usort callback function: function cmpcountry($a, $b) { $country1 = $a['country']; $country2 = $b['country']; if ($country1 == '') $country1 = "zzz"; if ($country2 == '') $country2 = "zzz"; return ($country1 < $country2) ? -1 : 1; } ...or, using ternary syntax: function cmpcountry($a, $b) { $country1 = ($a['country'] == '') ? "zzz" : $a['country']; $country2 = ($b['country'] == '') ? "zzz" : $b['country']; return ($country1 < $country2) ? -1 : 1; } Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting in array
And this for DESCending function cmpcountry($a, $b) { $country1 = $a['country']; $country2 = $b['country']; if($country1=='' OR $country2=='') { if($country1==$country2) return 0; elseif($country1=='') return 1; else return -1; } else return ($country1 < $country2) ? 1 : -1; } -Original Message- From: weetat [mailto:[EMAIL PROTECTED] Sent: Monday, July 31, 2006 3:15 PM To: php-general@lists.php.net; Paul Novitski Cc: php-general@lists.php.net Subject: Re: [PHP] sorting in array Thanks Paul, Very weird tried Peter's option, it doesn't work. Btw , how to sort by ascending ? Thanks Paul Novitski wrote: > At 12:22 AM 7/31/2006, Paul Novitski wrote: >> I could make that last statement just a bit simpler: >> >> function cmpcountry($a, $b) >> { >> $country1 = ($a['country'] == '') ? "zzz" : $a['country']; >> $country2 = ($b['country'] == '') ? "zzz" : $b['country']; >> >> return ($country1 > '' && $country1 < $country2) ? -1 : 1; >> } > > > *Sigh* I shouldn't post this late at night. This is the comparison > function that works for me: > > function cmpcountry($a, $b) > { > $country1 = ($a['country'] == '') ? "zzz" : $a['country']; > $country2 = ($b['country'] == '') ? "zzz" : $b['country']; > > return ($country1 < $country2) ? -1 : 1; > } > > demo: http://juniperwebcraft.com/test/sortTest.php > code: http://juniperwebcraft.com/test/sortTest.txt > > Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting in array
At 01:14 AM 7/31/2006, weetat wrote: Thanks Paul, Very weird tried Peter's option, it doesn't work. Btw , how to sort by ascending ? Please explain what you mean. The current script DOES sort ascending by country (except for the blank country fields which are placed at the end): http://juniperwebcraft.com/test/sortTest.php Singapore, Singapore, Thailand is an ascending sort. Your original code also sorted ascending by country, but with the blank countries at the front: function cmpcountry($a, $b) { $country1 = $a['country']; $country2 = $b['country']; return ($country1 < $country2) ? -1 : 1; } Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting in array
Thanks Paul, Very weird tried Peter's option, it doesn't work. Btw , how to sort by ascending ? Thanks Paul Novitski wrote: At 12:22 AM 7/31/2006, Paul Novitski wrote: I could make that last statement just a bit simpler: function cmpcountry($a, $b) { $country1 = ($a['country'] == '') ? "zzz" : $a['country']; $country2 = ($b['country'] == '') ? "zzz" : $b['country']; return ($country1 > '' && $country1 < $country2) ? -1 : 1; } *Sigh* I shouldn't post this late at night. This is the comparison function that works for me: function cmpcountry($a, $b) { $country1 = ($a['country'] == '') ? "zzz" : $a['country']; $country2 = ($b['country'] == '') ? "zzz" : $b['country']; return ($country1 < $country2) ? -1 : 1; } demo: http://juniperwebcraft.com/test/sortTest.php code: http://juniperwebcraft.com/test/sortTest.txt Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting in array
At 12:22 AM 7/31/2006, Paul Novitski wrote: I could make that last statement just a bit simpler: function cmpcountry($a, $b) { $country1 = ($a['country'] == '') ? "zzz" : $a['country']; $country2 = ($b['country'] == '') ? "zzz" : $b['country']; return ($country1 > '' && $country1 < $country2) ? -1 : 1; } *Sigh* I shouldn't post this late at night. This is the comparison function that works for me: function cmpcountry($a, $b) { $country1 = ($a['country'] == '') ? "zzz" : $a['country']; $country2 = ($b['country'] == '') ? "zzz" : $b['country']; return ($country1 < $country2) ? -1 : 1; } demo: http://juniperwebcraft.com/test/sortTest.php code: http://juniperwebcraft.com/test/sortTest.txt Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting in array
At 05:40 PM 7/30/2006, Peter Lauri wrote: function cmpcountry($a, $b) { $country1 = $a['country']; $country2 = $b['country']; if($country1=='') return 1; else return ($country1 < $country2) ? -1 : 1; } Good call, Peter; my suggestion was unnecessarily fat. Weetat, both Peter's and my solutions do work -- see here: http://juniperwebcraft.com/test/sortTest.php source code: http://juniperwebcraft.com/test/sortTest.txt I could make that last statement just a bit simpler: function cmpcountry($a, $b) { $country1 = ($a['country'] == '') ? "zzz" : $a['country']; $country2 = ($b['country'] == '') ? "zzz" : $b['country']; return ($country1 > '' && $country1 < $country2) ? -1 : 1; } I'm curious to know why none of this code works properly if it directly compares ($a['country'] < $b['country']). Why are the intermediate variables necessary? Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting in array
At 10:31 PM 7/30/2006, weetat wrote: I have problem when doing usort() when 'country' = '', i would like to display records where country = '' last. Any ideas how to do that ? ... $arraytest= array( array ( 'country' => '', ) , array ( 'country' => 'Thailand', ) ... function cmpcountry($a, $b) { $country1 = $a['country']; $country2 = $b['country']; return ($country1 < $country2) ? -1 : 1; } Weetat, You can sort the blank fields to the end simply by forcing their evaluated values in your usort callback function: function cmpcountry($a, $b) { $country1 = $a['country']; $country2 = $b['country']; if ($country1 == '') $country1 = "zzz"; if ($country2 == '') $country2 = "zzz"; return ($country1 < $country2) ? -1 : 1; } ...or, using ternary syntax: function cmpcountry($a, $b) { $country1 = ($a['country'] == '') ? "zzz" : $a['country']; $country2 = ($b['country'] == '') ? "zzz" : $b['country']; return ($country1 < $country2) ? -1 : 1; } Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting in array
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:[EMAIL PROTECTED] Sent: Monday, July 31, 2006 12:32 PM To: php-general@lists.php.net Subject: [PHP] 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.""; while(list($n,$v) = each($arraytest[$name])){ echo $v.""; } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting in array
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:[EMAIL PROTECTED] Sent: Monday, July 31, 2006 12:32 PM To: php-general@lists.php.net Subject: [PHP] 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.""; while(list($n,$v) = each($arraytest[$name])){ echo $v.""; } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] 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.""; while(list($n,$v) = each($arraytest[$name])){ echo $v.""; } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php