southofsomewhere Posted May 16, 2008 Share Posted May 16, 2008 Alright, I essentially have an array of objects, and I'm trying to figure out how it would be possible to sort the array (or even a portion of the array) by an instance variable in the object.. in the array. e.g. arr[0] => myObject->schoolID = 1337; arr[0] => myObject->age = 5; arr[0] => myObject->name = "mary"; arr[1] => myObject->schoolID = 1337; arr[1] => myObject->age = 2; arr[1] => myObject->name = "joe"; arr[2] => myObject->schoolID = 42; arr[2] => myObject->age = 2; arr[2] => myObject->name = "sue"; so in the above example, how could I go by taking all of the people with schoolID 1337, and sort them by age. Thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 16, 2008 Share Posted May 16, 2008 look into the usort() function and read it carefully cause I'm terrible at explaining it Quote Link to comment Share on other sites More sharing options...
southofsomewhere Posted May 16, 2008 Author Share Posted May 16, 2008 Did some looking into usort (actually had the php.net page open while reading your response), and I'm not sure if it works yet as I'm trying to figure out what the first value in the array function actually is (constructor.. class name.. object name..), and I don't quite get how its comparing since cmp_obj is never really called with two values to compare to. .. but I'm probably missing something. Edit: <?php // get all of the group objects associated with this orgID for($z = 0; $z < count($orgArr[$z])) { if($orgArr[$z]->orgID == $orgID) { $curOrgArr[$z] = $orgArr[$z]; } } I set it up so I don't even have to sort a portion of the array, but an entire array. So all thats left is sorting via a class member. Quote Link to comment Share on other sites More sharing options...
southofsomewhere Posted May 16, 2008 Author Share Posted May 16, 2008 Alright.. done to the point of.. I need to be able to sort by a given member (which should be fairly easy, but as stated before I can't find where cmp_obj's param's are being passed.) <?php // get all of the group objects associated with this orgID for($z = 0; $z < count($orgArr); $z++) { // if the current objects orgID is equal to the orgID we're wanting to sort if($orgArr[$z]->orgID == $orgID) { // put the object in the new array $curOrgArr[$z] = $orgArr[$z]; } } usort($curOrgArr, array("OrganizationViewManager", "cmp_obj")); ?> is what I have for the initializing array/sorting.. and then the cmp_obj function, pulled from php.net.. <?php static function cmp_obj($a, $b) { $al = strtolower($a->numStudents); $bl = strtolower($b->numStudents); if ($al == $bl) { return 0; } return ($al > $bl) ? +1 : -1; } ?> last issue: I need to be able to change $a->numStudents and $b->numStudents.. and the way I could think of doing it is adding an extra param.. but as you see (or at least I dont), I don't see where one could do this. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.