Re: sorting associative array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jochem Maas wrote:


...



This won't work for me as I have 500+ records to sort based on the score key.. looking at jochem's class now. Thanks




which wont help much - assuming what you say is true
(why wont it work? have you tried it).

sort method from the class:

    function sort()
    {
       if(count($this->sortKeys)) {
           usort($this->dataArray, array($this,"_sortcmp"));
       }
    }


Thanks jochem, I had a second look at it and I figured it out. I had problem implementing it with a class but now I have figured how to use it.

cheers,
Jeffery


ok, does that mean using usort() on your array works? my class is just a
fancy wrapper around usort - and ofcourse it allows you to sort on more than
1 'column' in the second dimension of the array (kind of like a multi-column
sort in an SQL statement). -- the point being that the usort() example Kurt
(I think that was his name) sent would work also.


anyway glad you could use it. :-)

....

Yes the example sent by Kurt Yoder worked for me. I coudn't work out the errors with the class you sent me. I realised it was written for PHP5 in mind ?... or maybe I wasn't patient enough to spent time debugging it :-(

/**
* This function is to sort a multi-dimentional array. This is a call-back function.
* Replace the value of $this->mArraySortKey with the appropriate key
* before calling the call-back function
*/
function sort_array($x, $y)
{
if ( $x[$this->mArraySortKey] == $y[$this->mArraySortKey] )
return 0;
else if ( $x[$this->mArraySortKey] < $y[$this->mArraySortKey] )
return 1;
else
return -1;
}


and within my class I am calling the following two lines:

   // Sort the array
   $this->mArraySortKey = 'score';
   usort($this->mSearchData, array(& $this, 'sort_array'));


cheers, Jeffery Fernandez http://melbourne.ug.php.net

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux