Sorting arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
IHeartVVS
Forum Newbie
Posts: 1
Joined: Fri Jul 23, 2010 1:19 am
Location: Denmark

Sorting arrays

Post 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
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Sorting arrays

Post 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;
}
Post Reply