RE: [PHP] sorting in array

From: Date: Mon, 31 Jul 2006 07:22:17 +0000
Subject: RE: [PHP] sorting in array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-239893@lists.php.net to get a copy of this message
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'] == '') ? "zzzzzzz" : $a['country'];
        $country2 = ($b['country'] == '') ? "zzzzzzz" : $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

« previous php.general (#239893) next »