473,503 Members | 11,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting an associative array of objects by value?

Hi all,

I'm trying to sort an array of objects within an object. Included is a
dumbed down example so that we can get at the meat of the issue without
worrying about complexity or validation. Basically, Funk is an object that
has a name (string) and a value(int). FunkThis represents an object
containing a list of Funks, with each key corresponding to the name of the
Funk. I want to sort this list by val, but I get a Warning: usort():
Invalid comparison function error when I run the script. Keep in mind that
this is for PHP 4

I'm open to suggestions to getting this to work.

<?php

class Funk {

var $name;
var $val;

function Funk($name,$val) {
$this->name = $name;
$this->val = $val;
}

function getVal() {
return $this->val;
}

function getName() {
return $this->name;
}
}

class FunkThis {

var $list;

function FunkThis() {
$this->list = null;
}

function add($funk) {
$this->list[$funk->getName()] = $funk;
}

function compareFunks($a,$b) {
$aVal = $a->getVal();
$bVal = $b->getVal();
if ($aVal == $bVal) return 0;
if ($aVal $bVal) return 1;
if ($aVal < $bVal) return -1;

}

function sortByFunk() {
$list = $this->list;
$this->list = uasort($list,"compareFunks");
}

function show() {
print_r($this->list);
}
}

$a = new Funk("A",3);
$b = new Funk("B",7);
$c = new Funk("C",1);
$d = new Funk("D",9);
$e = new Funk("E",6);

$funkthat = new FunkThis();

$funkthat->add($a);
$funkthat->add($b);
$funkthat->add($c);
$funkthat->add($d);
$funkthat->add($e);

$funkthat->sortByFunk();

$funkthat->show();
?>

Greg
Dec 12 '06 #1
3 3309
..oO(Greg D)
>I'm trying to sort an array of objects within an object. Included is a
dumbed down example so that we can get at the meat of the issue without
worrying about complexity or validation. Basically, Funk is an object that
has a name (string) and a value(int). FunkThis represents an object
containing a list of Funks, with each key corresponding to the name of the
Funk. I want to sort this list by val, but I get a Warning: usort():
Invalid comparison function error when I run the script. Keep in mind that
this is for PHP 4
[...]
callback
http://www.php.net/manual/en/languag...types.callback

| A method of an instantiated object is passed as an array containing an
| object as the element with index 0 and a method name as the element
| with index 1.

Since compareFunks() is a method of FunkThis, you need

uasort($list, array($this, 'compareFunks'));

to call it.

Micha
Dec 13 '06 #2

"Michael Fesser" <ne*****@gmx.dewrote in message
news:ni********************************@4ax.com...
.oO(Greg D)
>>I'm trying to sort an array of objects within an object. Included is a
dumbed down example so that we can get at the meat of the issue without
worrying about complexity or validation. Basically, Funk is an object
that
has a name (string) and a value(int). FunkThis represents an object
containing a list of Funks, with each key corresponding to the name of the
Funk. I want to sort this list by val, but I get a Warning: usort():
Invalid comparison function error when I run the script. Keep in mind
that
this is for PHP 4
[...]

callback
http://www.php.net/manual/en/languag...types.callback

| A method of an instantiated object is passed as an array containing an
| object as the element with index 0 and a method name as the element
| with index 1.

Since compareFunks() is a method of FunkThis, you need

uasort($list, array($this, 'compareFunks'));

to call it.
Ah, see, I had done that, and when invoking FunkThis->compareFunks(), it
printed 1.

I then used uasort($this->list, array($this, 'compareFunks'));

and it worked fine. Must be because the function parameter is passing the
array by reference.

Thanks!

Greg


Dec 13 '06 #3

"Greg D" <gr***@lasmag.comwrote in message
news:EZ*****************@newsfe24.lga...
>
"Michael Fesser" <ne*****@gmx.dewrote in message
news:ni********************************@4ax.com...
>.oO(Greg D)
>>>I'm trying to sort an array of objects within an object. Included is a
dumbed down example so that we can get at the meat of the issue without
worrying about complexity or validation. Basically, Funk is an object
that
has a name (string) and a value(int). FunkThis represents an object
containing a list of Funks, with each key corresponding to the name of
the
Funk. I want to sort this list by val, but I get a Warning: usort():
Invalid comparison function error when I run the script. Keep in mind
that
this is for PHP 4
[...]

callback
http://www.php.net/manual/en/languag...types.callback

| A method of an instantiated object is passed as an array containing an
| object as the element with index 0 and a method name as the element
| with index 1.

Since compareFunks() is a method of FunkThis, you need

uasort($list, array($this, 'compareFunks'));

to call it.

Ah, see, I had done that, and when invoking FunkThis->compareFunks(), it
printed 1.

I then used uasort($this->list, array($this, 'compareFunks'));

and it worked fine. Must be because the function parameter is passing the
array by reference.

Thanks!
Actually, that's not quite right either (my bad). I didn't read the
function description fully. uasort affects the array and returns a boolean.
D'oh!

This would do it:

function sortByFunk() {
uasort($this->list, array($this,"compareFunks"));
}

and so would this:

function sortByFunk() {
$newlist = $this->list;
uasort($newlist, array($this,"compareFunks"));
$this->list = $newlist;
}

What can I say, I'm an idiot. :)

GregoryD
Dec 13 '06 #4

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

Similar topics

27
11120
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
1997
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
2653
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
7673
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
5028
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
5
37681
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
35
6591
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
7
39827
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
4874
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
0
7212
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
7098
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
7296
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,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.