Recursive usort does not work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
darkdss
Forum Newbie
Posts: 11
Joined: Thu Nov 06, 2008 12:40 pm

Recursive usort does not work?

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Recursive usort does not work?

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Recursive usort does not work?

Post 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 ;)
Last edited by VladSun on Thu Nov 06, 2008 1:15 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Recursive usort does not work?

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Recursive usort does not work?

Post by VladSun »

Hm, maybe http://bg.php.net/manual/en/function.cr ... nction.php will be more appropriate in this case.
There are 10 types of people in this world, those who understand binary and those who don't
darkdss
Forum Newbie
Posts: 11
Joined: Thu Nov 06, 2008 12:40 pm

Re: Recursive usort does not work?

Post by darkdss »

Hmm I'll try that.

Does anyone know if usort is faster than array_multisort?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Recursive usort does not work?

Post by VladSun »

Test it ;)
There are 10 types of people in this world, those who understand binary and those who don't
darkdss
Forum Newbie
Posts: 11
Joined: Thu Nov 06, 2008 12:40 pm

Re: Recursive usort does not work?

Post 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');
    }
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Recursive usort does not work?

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Recursive usort does not work?

Post 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).
darkdss
Forum Newbie
Posts: 11
Joined: Thu Nov 06, 2008 12:40 pm

Re: Recursive usort does not work?

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Recursive usort does not work?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply