Page 1 of 1

Recursive usort does not work?

Posted: Thu Nov 06, 2008 12:55 pm
by darkdss
Hi guys.
I have a recursive function that needs to usort in a class:

Code: Select all

public function sortme($data) {
    function cmp($a, $b)
    {
        if ($a == $b) {
           return 0;
     }
        return ($a < $b) ? -1 : 1;
    }
 
    $data = usort($data, "cmp");
    return $data;
}
 
But I can't do this becuase every time it calls itself again it throws me an error saying "function cmp cannot be redefined".

Does anyone know how I can usort within a class?

thanks

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 1:09 pm
by Eran

Code: Select all

 
class foo {
    public function sortme($data) {
          usort($data,array('foo','cmp'));
         return $data;
    }
    public static function cmp($a,$b) {
       if ($a == $b) {
             return 0;
       }
       return ($a < $b) ? -1 : 1;
    }
}
 
Note also that usort operates by reference and returns a boolean - the way you wrote it the array gets overwritten.

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 1:10 pm
by VladSun
Define the cmp function externaly (i.e. not nested)
EDIT: Follow pytrin's suggestion, but I would have defined the cmp function as private (also I'm not sure why it should be static - non static function will work the same way).

EDIT2: Oh, I think I see now the "benefits" of a publis static function in this case ;)

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 1:13 pm
by Eran
You are right, for some reason I thought it would have to be public to be called in this manner... but it doesn't
It has to be static though

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 1:17 pm
by VladSun
Hm, maybe http://bg.php.net/manual/en/function.cr ... nction.php will be more appropriate in this case.

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 1:48 pm
by darkdss
Hmm I'll try that.

Does anyone know if usort is faster than array_multisort?

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 1:52 pm
by VladSun
Test it ;)

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 2:16 pm
by darkdss
Damn it doesn't work. For me it says:
Warning: usort() [function.usort]: Invalid comparison function.

Does anyone know why?
(This code is all in the same class)

Code: Select all

 
private function sort($list, $axis) {
        switch($axis) {
            case 'x':
                usort($list, array('sort','cmp'));
                break;
        }
 
return $list;
    }
    
    private static function cmp($a, $b) {
        if($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : +1;
    }
 
    public function runTest() {
        $this->sort($data, 'x');
    }

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 3:11 pm
by VladSun
I think the function must be static and public.
http://bg.php.net/usort
Example #3 usort() example using a member function of an object

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 3:34 pm
by Eran
The first item in the array for the usort callback should be the class name, make sure that is indeed that (not the calling function name).

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 4:52 pm
by darkdss
pytrin wrote:The first item in the array for the usort callback should be the class name, make sure that is indeed that (not the calling function name).
Bingo
thanks

Do you know this is quicker than muiltisort?

Re: Recursive usort does not work?

Posted: Thu Nov 06, 2008 5:14 pm
by pickle
darkdss wrote:Does anyone know if usort is faster than array_multisort?
That depends on your dataset & the efficiency of your sorting algorithm. If I had to guess, I would say sorting with built-in functions like array_multisort(), would be faster.