Re: sorting associative array

From: Date: Mon, 24 Jan 2005 12:30:05 +0000
Subject: Re: sorting associative array
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-207115@lists.php.net to get a copy of this message
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

« previous php.general (#207115) next »