CodeIgniter Forums
Trying to call uasort from within a model method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Trying to call uasort from within a model method (/thread-31176.html)



Trying to call uasort from within a model method - El Forum - 06-09-2010

[eluser]blcArmadillo[/eluser]
I'm trying to call a custom sort function from within a method in one of my models so I have something like:

Code:
<?php
class Part_model extends Model {
    
    function __construct()
    {
        parent::Model();
    }

    function my_sort($a, $b)
    {
        // sort code
    }

    function some_method()
    {
        $anarray = array(...);
        uasort($parts, 'my_sort');
    }

Problem is I get the following error:
Quote:uasort() expects parameter 2 to be a valid callback, function 'my_sort' not found or invalid function name.

I'd imagine this is because just like if you were calling the function normally from the method you'd use something like $this->my_sort('aval', 'bval'). I tried passing '$this->my_sort' into the second argument but that didn't work either. Any ideas?


Trying to call uasort from within a model method - El Forum - 06-09-2010

[eluser]Rob Gordijn[/eluser]
look at example #3 here.
you can see you must pass an array for a cmp function that sits in a class.

but my thoughts: move your compare function to a helper and load it.

cheers!


Trying to call uasort from within a model method - El Forum - 06-09-2010

[eluser]blcArmadillo[/eluser]
Is example three really the same situation? I'm trying to call sort from within one of my methods. In example three they're not calling sort from a method so they just follow the standard usage since the methods are in the local scope... right?


Trying to call uasort from within a model method - El Forum - 06-09-2010

[eluser]Rob Gordijn[/eluser]
well, I don't think the scope is the same... uasort expects a valid callback. Normal usage is a 'normal' function, not a method within the class.

maybe is array($this,'my_sort'); valid, but I doubt it since $this should be a string.
maybe you can use __CLASS__ for the current classname (but does calling uasort then create a new object of the class?)

[edit] weird language mixups


Trying to call uasort from within a model method - El Forum - 06-09-2010

[eluser]danmontgomery[/eluser]
http://php.net/manual/en/function.uasort.php

Code:
//Note use of array to reference member method of this object in callback
uasort($this->dataSet,array($this,"cmpName"));



Trying to call uasort from within a model method - El Forum - 06-09-2010

[eluser]mddd[/eluser]
To add to Rob Gordijn and noctrum: you can specify either a class instance (object) or a class name (string). Both are okay.


Trying to call uasort from within a model method - El Forum - 06-09-2010

[eluser]blcArmadillo[/eluser]
Hey everyone,
Thanks for all the help. I used the snippet that noctrum posted and everything is working!