Forum Moderators: coopster

Message Too Old, No Replies

sorting multidimensional array

sorting multidimensional array

         

m_leefs

12:00 am on Dec 25, 2004 (gmt 0)

10+ Year Member



I am sorting a multidimensional array ($listing), using the following code:


function compare ($x, $y) {
if ( $x[1] == $y[1] )
return 0;
else if ( $x[1] < $y[1] )
return -1;
else
return 1;
}
usort ($listing, 'compare');

However, I would like to set up a sorting function in which I can dynamically select which value to sort by. So I tried multiple varients of the below code, but I can't seem to get the "$i" varible to work. Is there a way around this, or a better script out there?


$i = 1; // or what ever value I select

function compare ($x, $y) {
if ( $x[$i] == $y[$i] )
return 0;
else if ( $x[$i] < $y[$i] )
return -1;
else
return 1;
}
usort ($listing, 'compare');

Oh and Merry Christmas!

mincklerstraat

8:06 am on Dec 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd have a look at
array_multisort() [be.php.net]
.