[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Sorting an array of objects - OOP PHP

Posted on 2007-07-31
Medium Priority
224 Views
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
0
Question by:bergstrom_davin

LVL 9

Accepted Solution

Rurne earned 900 total points
ID: 19605290
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);

http://us.php.net/manual/en/function.usort.php
0

LVL 9

Assisted Solution

blue_hunter earned 600 total points
ID: 19606895
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 ;

ksort(\$obj_array);
\$obj_array = array_values(\$obj_array);

0

LVL 1

Author Comment

ID: 19609311
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

// 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

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
###### Suggested Courses
Course of the Month18 days, 22 hours left to enroll