Sorting an array of objects - OOP PHP

Posted on 2007-07-31
Last Modified: 2008-02-01
I would like to sort an array of objects by a variable of an object variable.

I have an array of objects (obj_array).
One of the variables of (obj_a) is an object (obj_b).
I want to sort by a variable of (obj_b).

So I want to sort an
$obj_array = array (
      $butter = new obj_a,
      $flour = new obj_a,
      $water = new obj_a
by obj_a->obj_b->volume

where obj_a has a variable object called obj_b and obj_b has variable called volume
Question by:bergstrom_davin
    LVL 9

    Accepted Solution

    To do that, you're going to need to define a side function for basic comparison.  With that defined, you then sort using usort().

    function mySort($a, $b){
        $sub_object_a = $a->obj_b;
        $sub_object_b = $b->obj_b;

        if($sub_object_a->volume == $sub_object_b->volume){
           return 0;

        return ($sub_object_a->volume > $sub_object_b->volume) ? 1 : -1;

    usort($obj_array, "mySort);

    More Info:
    LVL 9

    Assisted Solution

    see if you can arrange your array as below

    $butter = new obj_a;
    $flour = new obj_a;
    $water = new obj_a;

    $obj_array = array();
    $obj_array[$butter->obj_b->volume] = $butter;
    $obj_array[$flour->obj_b->volume] = $flour;
    $obj_array[$water->obj_b->volume] = $water ;

    $obj_array = array_values($obj_array);

    LVL 1

    Author Comment

    Thank you Rurne, but I was still having trouble with sorting objects of objects by something other than volume.

    Thank you blue_hunter, I liked your approach but I would lose $butter or $water if they had the same volume.

    Below is a working solution I put together from both your ideas.

    function sortby ($sortby = "linkid") {
      // key by sort
      foreach ($this->links as $key => $link) $sorted[$key] = $link->original->{$sortby};

      // sort by key if this sort and last sort are the same then reverse sort
      if ($sortby == $this->sortby) arsort($sorted);      // up
      else asort($sorted);                  // down

      // rekey with id
      foreach ($sorted as $key => $value) $rekeyed[$key] = $this->links[$key];
      // save sorted array
      $this->links = $rekeyed;

      // update sort for use in next pass to determine direction
      if ($sortby != "linkid") $this->sortby = $sortby;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now