Jump to content

Sorting Objects in an Array


Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.