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 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
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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";
|
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...
|
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...
|
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...
|
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...
| |
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:...
|
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>
|
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);
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |