|
Posted by Greg D on 12/13/06 15:33
"Greg D" <gregd@lasmag.com> wrote in message
news:EZUfh.5656$a14.2858@newsfe24.lga...
>
> "Michael Fesser" <netizen@gmx.de> wrote in message
> news:niiun2tqj9n2rhhf4meql2e95oau2vinis@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/language.pseudo-types.php#language.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
[Back to original message]
|