Page 1 of 1

Sorting arrays

Posted: Fri Jul 23, 2010 1:56 am
by IHeartVVS
Hi all,

I remember from java, that an array containing objects with the method compareTo implemented to define a sequence order, could be sorted by the standard array sorting function. I've been looking for a similar functionality/method in PHP, and was wondering if anyone here knows anything?

Cheers,
Thomas

Re: Sorting arrays

Posted: Fri Jul 23, 2010 4:11 am
by pbs
This is the 2 dimensional sorting function, may this will help you.

Code: Select all

function sort_array($array, $key, $order)
{
	if ($order=="DESC")
		$or="arsort";
	else
		$or="asort";
	for ($i = 0; $i < sizeof($array); $i++) 
	{
		$sort_values[$i] = $array[$i][$key];
	} 
$or($sort_values);
	reset ($sort_values);
	while (list ($arr_key, $arr_val) = each ($sort_values)) 
	{
		$sorted_arr[] = $array[$arr_key];
	}
	return $sorted_arr;
}