473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How do you sort an array of objects?

The PHP Manual offered me NO help on this one, so now I appeal to the
populace:

[PHP]
$dirID = @opendir('/my/path/to/files');
while (($fyl = @readdir($dirID))) {
$obj =& new ClassObject($fyl);
array_push($imageArray, $obj);
$obj = null;
}
[/PHP]

I now have an array $imageArray of objects as so:

Expand|Select|Wrap|Line Numbers
  1. $imageArray[0]->image_name
Expand|Select|Wrap|Line Numbers
  1. $imageArray[0]->image_dimensions
I need to sort $imageArray by "image_name" or by "image_dimensions"
and I can't think of a way to do it, any ideas?

Thanx
Phil
Jul 17 '05 #1
4 2152
Phil Powell wrote:
The PHP Manual offered me NO help on this one, so now I appeal to the
populace:

[PHP]
$dirID = @opendir('/my/path/to/files');
while (($fyl = @readdir($dirID))) {
$obj =& new ClassObject($fyl);
array_push($imageArray, $obj);
$obj = null;
}
[/PHP]

I now have an array $imageArray of objects as so:

Expand|Select|Wrap|Line Numbers
  1. $imageArray[0]->image_name
Expand|Select|Wrap|Line Numbers
  1. $imageArray[0]->image_dimensions

I need to sort $imageArray by "image_name" or by "image_dimensions"
and I can't think of a way to do it, any ideas?

Thanx
Phil


have a look at the user comments in the PHP Manual on the pages about
array sorting including...

http://uk2.php.net/manual/en/function.asort.php

~Cameron
Jul 17 '05 #2
asort() does not do anything that I would want since I do not want the
key-val association to be retained. I looked into usort() but that
failed because the callback function that usort calls never gets
called...

$functionBundleArray[0] = get_class($this);
$functionBundleArray[1] = 'field_name_sort';
usort($imageArray, $functionBundleArray, $fieldName);

....

function field_name_sort(&$a, &$b, $fieldName) {
return strcmp(strtolower($a->$fieldName),
strtolower($b->$fieldName));
}

Problem is that $a and $b need to be compared by index, and I have
absolutely NO idea how to do that since usort() doesn't allot for
using any index at all.

Phil

Cameron <fo*@bar.invalid> wrote in message news:<c0**********@news6.svr.pol.co.uk>...
Phil Powell wrote:
The PHP Manual offered me NO help on this one, so now I appeal to the
populace:

[PHP]
$dirID = @opendir('/my/path/to/files');
while (($fyl = @readdir($dirID))) {
$obj =& new ClassObject($fyl);
array_push($imageArray, $obj);
$obj = null;
}
[/PHP]

I now have an array $imageArray of objects as so:

Expand|Select|Wrap|Line Numbers
  1. $imageArray[0]->image_name
Expand|Select|Wrap|Line Numbers
  1. $imageArray[0]->image_dimensions

I need to sort $imageArray by "image_name" or by "image_dimensions"
and I can't think of a way to do it, any ideas?

Thanx
Phil


have a look at the user comments in the PHP Manual on the pages about
array sorting including...

http://uk2.php.net/manual/en/function.asort.php

~Cameron

Jul 17 '05 #3
Phil Powell wrote:
asort() does not do anything that I would want since I do not want the
key-val association to be retained. I looked into usort() but that
failed because the callback function that usort calls never gets
called...

$functionBundleArray[0] = get_class($this);
$functionBundleArray[1] = 'field_name_sort';
usort($imageArray, $functionBundleArray, $fieldName); usort takes /two/ parameters
http://www.php.net/usort
function field_name_sort(&$a, &$b, $fieldName) {
return strcmp(strtolower($a->$fieldName),
strtolower($b->$fieldName));
} the callback function takes /two/ parameters.
Problem is that $a and $b need to be compared by index, and I have
absolutely NO idea how to do that since usort() doesn't allot for
using any index at all.

So you need to pass a /third/ parameter to the callback function?

Use a global variable :-)

something like this:

#v+
<?php
function user_sort($a, $b) {
global $GLOBAL_VARIABLE_USED_FOR_USER_SORT;

// get a better *local* name :-)
$x = $GLOBAL_VARIABLE_USED_FOR_USER_SORT;
if ($a->$x == $b->$x) return 0;
return ($a->$x < $b->$x) ? -1 : 1;
}

function show($a, $title='') {
echo "$title:\n";
foreach ($a as $k=>$v) {
echo "[$k]";
foreach ($v as $kk=>$vv) echo " $kk = $vv;";
echo "\n";
}
echo "\n\n";
}

$a[0]->fullname = 'Zapotec'; $a[0]->age = 30;
$a[1]->fullname = 'Michael'; $a[1]->age = 80;
$a[2]->fullname = 'Abigail'; $a[2]->age = 17;

show($a);

$GLOBAL_VARIABLE_USED_FOR_USER_SORT = 'fullname';
usort($a, 'user_sort');
show($a, 'sorted by fullname');

$GLOBAL_VARIABLE_USED_FOR_USER_SORT = 'age';
usort($a, 'user_sort');
show($a, 'sorted by age');

echo "\n\n";
?>
#v-
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4
uksort()

Uzytkownik "Phil Powell" <so*****@erols.com> napisal w wiadomosci
news:1c**************************@posting.google.c om...
asort() does not do anything that I would want since I do not want the
key-val association to be retained. I looked into usort() but that
failed because the callback function that usort calls never gets
called...

$functionBundleArray[0] = get_class($this);
$functionBundleArray[1] = 'field_name_sort';
usort($imageArray, $functionBundleArray, $fieldName);

...

function field_name_sort(&$a, &$b, $fieldName) {
return strcmp(strtolower($a->$fieldName),
strtolower($b->$fieldName));
}

Problem is that $a and $b need to be compared by index, and I have
absolutely NO idea how to do that since usort() doesn't allot for
using any index at all.

Phil

Cameron <fo*@bar.invalid> wrote in message

news:<c0**********@news6.svr.pol.co.uk>...
Phil Powell wrote:
The PHP Manual offered me NO help on this one, so now I appeal to the
populace:

[PHP]
$dirID = @opendir('/my/path/to/files');
while (($fyl = @readdir($dirID))) {
$obj =& new ClassObject($fyl);
array_push($imageArray, $obj);
$obj = null;
}
[/PHP]

I now have an array $imageArray of objects as so:

Expand|Select|Wrap|Line Numbers
  1. $imageArray[0]->image_name
Expand|Select|Wrap|Line Numbers
  1. $imageArray[0]->image_dimensions

I need to sort $imageArray by "image_name" or by "image_dimensions"
and I can't think of a way to do it, any ideas?

Thanx
Phil


have a look at the user comments in the PHP Manual on the pages about
array sorting including...

http://uk2.php.net/manual/en/function.asort.php

~Cameron

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
7
by: Daniel | last post by:
does C# have any collection objects that support sort functionality so that I dont have to write my own sorting algorithm?
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: fdu.xiaojf | last post by:
Hi, It seems that an array acts like an list very much, except it doesn't have a method sort. Regards,
2
by: John Devlon | last post by:
Hi, I've created my own class containing a few properties like name, zip-code and savings. At some point i'm storing several objects in an array. Does anyone know how to sort the array using...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.