PHP usort won't sort class
Asked Answered
A

3

17

This is a sample of the array of elemnts to sort:

$items = 
    array
      0 => 
        object(stdClass)[8]
          public 'id' => string '110' (length=3)
          public 'brand_id' => string '18' (length=2)
            array
              0 => string ' OT-708' (length=7)
          public 'failed' => null
          public 'diff' => null
      1 => 
        object(stdClass)[9]
          public 'id' => string '161' (length=3)
          public 'brand_id' => string '18' (length=2)

So, let's say I want to sort by brand_id. This is my usort callback function:

function _compare($itemA, $itemB){

    if ($itemA->brand_id == $itemB->brand_id) {

        return 0; 
    }
    else{
        return strcmp($itemA->brand_id, $itemB->brand_id); //just an example...
    }

}

And when I do usort($items, '_compare'); var_dump($items); nothing happens. Any clues on how to troubleshoot this?

--UPDATE--

Ok, I've simplified the problem to this:

function cmp($itemA, $itemB){
    return -1;
}

if (usort($items, "cmp"))
                echo 'I just sorted!';
else echo 'Cant sort!';

It always prints 'Cant sort!'

Angelaangele answered 21/6, 2011 at 2:47 Comment(11)
brand_name doesn't even exist in those objects.Hurricane
Are you printing the return of usort or $items? because usort only returns a bool on success / false otherwise.Tyler
Sorry, guys, I corrected the example.Angelaangele
I was printing $items. I'll check the compare function return.Angelaangele
I've updated the question, usort always fails for me.Angelaangele
@ign it's if (usort($items, "cmp")), NOT if (usort(items, "cmp")), rihgt?Pubescent
Yes, $items, sorry, I already feel like an idiot.Angelaangele
@ign you could do var_dump($items); before usort? Does it show anything? Or could you show us more of your code?Pubescent
OK, I figured it out... My mistake for thinking I had error reporting ON. All this code, including function cmp is inside a class, so I was getting this and didn't realize "( ! ) Warning: usort() expects parameter 2 to be a valid callback, function 'cmp' not found or invalid function name in ..."Angelaangele
@ign , nice you could now insert your problem solution as an answer and accept it for future readers that could have a similar problem.Pubescent
Yeah, I will as soon as SO lets me. Thanks for your help, it would have taken me a while more without it.Angelaangele
A
49

Finally, I discovered the source of this error. The problem was that this code was inside a class.

If that's your case, then you should call usort this way:

usort($items, array("MyClass", "compare_method"));

Furthermore, if your Class is in a namespace, you should list the full namespace in usort.

usort($items, array('Full\Namespace\WebPageInformation', 'compare_method'));
Angelaangele answered 21/6, 2011 at 4:17 Comment(3)
Thanks, got the same problem with uksort. The documentation says uksort will return true or false, and it was returning null.Cyclohexane
If anyone else is still having issues, try making your compare function static.Binturong
You can also replace the class name with $this as the first element in the array. This is equiv to $this->compare_method($a, $b) so make sure the function is set to public. Otherwise usort is trying to do MyClass::comapre_method($a, $b) so make sure the function is set to static.Veta
W
2

Also, you can set a static function inside your Class:

static myfunction($a, $b){'yoursort'}

and call it like this:

usort($items, "Class::myfunction");
Weigel answered 23/6, 2014 at 8:38 Comment(0)
U
2

You can also use $this if you use usort in a Class:

usort($my_array, array($this, 'orderByDate'));
Urbanize answered 10/3, 2021 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.