setting a callback method instead of a function for usort

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

setting a callback method instead of a function for usort

Post by Luke »

How would I supply php with a class and method to sort by instead of a function for the usort and friends functions?

I need to be able to sort an array like the following, by username, first name, last name, and a few others:

Code: Select all

Array
(
    [4] => Model_User Object
        (
            [_table] => users
            [_lastSave:private] => 
            [_originalPassword:private] => 
            [_dbat] => 0
            [_tableat] => users
            [_where] => 
            [_saved] => 
            [_lasterr] => 
            [_original] => 
            [id] => 4
            [username] => stevebobbyman
            [password] => 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
            [last_login] => 
            [group_id] => 2
            [first_name] => SteveBobby
            [middle_name] => 
            [last_name] => None
            [nick_name] => 
            [spouse_name] => 
            [home_phone] => 
            [home_fax] => 
            [cell_phone] => 
            [personal_email] => 
            [home_address] => 
            [home_city] => 
            [home_state] => 
            [home_zip] => `
            [gender] => 
            [join_date] => 2007-01-01
            [birth_date] => 1980-01-15
            [photo] => 
            [business_name] => Name of Business
            [business_phone] => 1234567890
            [business_phone2] => 
            [business_email] => larry@stevebobby.com
            [business_website] => 
            [business_address] => 1234 Some Rd.
            [business_city] => City
            [business_state] => St
            [business_zip] => 12345
            [business_classification] => Classification
            [business_keywords] => word, internet, cancer, polution, monkey, turtle, sister, billy, stevebobby
            [business_description] => I am a description. This is where stuff goes about me.
            [last_modified] => 2007-01-01 14:39:32
            [date_created] => 2006-07-06 14:39:36
            [active] => 1
        )

    [3] => Model_User Object
        (
            [_table] => users
            [_lastSave:private] => 
            [_originalPassword:private] => 
            [_dbat] => 0
            [_tableat] => users
            [_where] => 
            [_saved] => 
            [_lasterr] => 
            [_original] => 
            [id] => 3
            [username] => tompers
            [password] => f53b845ba02c69f623d259ee8fc9da94fc2b905a
            [last_login] => 
            [group_id] => 2
            [first_name] => Jerry
            [middle_name] => 
            [last_name] => Jones
            [nick_name] => 
            [spouse_name] => 
            [home_phone] => 2147483647
            [home_fax] => 
            [cell_phone] => 
            [personal_email] => 
            [home_address] => 
            [home_city] => 
            [home_state] => 
            [home_zip] => 
            [gender] => f
            [join_date] => 1980-03-28
            [birth_date] => 1928-01-28
            [photo] => 
            [business_name] => Some Business
            [business_phone] => 2147483647
            [business_phone2] => 
            [business_email] => somebody@something.com
            [business_website] => 
            [business_address] => 1234 Fake Road
            [business_city] => Chicago
            [business_state] => IL
            [business_zip] => 54187
            [business_classification] => 1
            [business_keywords] => donkey, monkey, rabbit, turtle, astronaut, pilot, stereo, web, internet
            [business_description] => We are a business that does things like eat bacon and send stuff to other countries like apples and beans.
            [last_modified] => 2007-01-04 13:32:19
            [date_created] => 2007-01-04 13:32:19
            [active] => 1
        )

)
will usort even work here, or do I need another function?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

are you looking for:
http://www.php.net/manual/en/function.usort.php wrote:Example 3. usort() example using a member function of an object

Code: Select all

class TestObj {
   var $name;

   function TestObj($name)
   {
       $this->name = $name;
   }

   /* This is the static comparing function: */
   function cmp_obj($a, $b)
   {
       $al = strtolower($a->name);
       $bl = strtolower($b->name);
       if ($al == $bl) {
           return 0;
       }
       return ($al > $bl) ? +1 : -1;
   }
}

$a[] = new TestObj("c");
$a[] = new TestObj("b");
$a[] = new TestObj("d");

usort($a, array("TestObj", "cmp_obj"));

foreach ($a as $item) {
   echo $item->name . "\n";
}
:?:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

cool... now I can dynamically sort by whatever I want... :-D

Code: Select all

public function sort(Array $members, $sortBy)
    {
        // Check that members can be sorted by provided value
        if (!in_array($sortBy, $this->_canSortBy))
        {
            throw new Model_Exception('Can\'t sort by ' . $sortBy);
        }
        // Sort the members!
        usort($members, array($this, '_sort_' . $sortBy));
        return $members;
    }
    
    public function __call($method, $args)
    {
        // Check to see if 'sort' is in the method name
        if (strpos($method, 'sort'))
        {
            // Chop _sort_ off the beginning of the method name
            $count = strlen('_sort_');
            $sortBy = substr($method, 6);
            
            $a = $args[0];
            $b = $args[1];
            
            if ($a->$sortBy == $b->$sortBy) return 0;
            else return ($a->$sortBy > $b->$sortBy) ? 1 : -1;
            
        }
        // If it isn't pass this to parent's __call method
        else
        {
            return parent::__call($method, $args);
        }
    }
Post Reply