Location: PHPKode > scripts > EZ Framework > ez-framework/sorting.php
<?php
# Version 1.0

/**
	$sorting = new sorting();
	$t = $sorting->sort_list($ar,'two','one'); (Array, key1,key2,key3,ect.)
	$sorting->reverse();						Reverse The Sort
	$sorting->sort_by = 'arsort';				Change the sort method 
												Sort Methods (Below)
											->	natsort, natcasesort, sort, rsort, 
											->	arsort, asort, krsort, ksort
**/
class sorting
{
	var $sort_by;
	var $flags;
	var $reverse = false;
	function sorting($sort_by = 'natcasesort',$flags = '')
	{
		$this->sort_by = $sort_by;
		$this->flags = $flags;
	}
	function reverse()
	{
		if($this->reverse) 	$this->reverse = false;
		else				$this->reverse = true;			
	}
	function sort_list($array)
	{
	   // for all arguments without the first starting at end of list
	   for ($i=func_num_args(); $i>1; $i--)
	   {
		   // get column to sort by
		   $sort_by = func_get_arg($i-1);
		   // clear arrays
		   $new_array = array();
		   $temporary_array = array();
		   // walk through original array
		   foreach($array as $original_key => $original_value)
		   {
			   // and save only values
			   $temporary_array[] = $original_value[$sort_by];
		   }
		   // sort array based on the sort type
		  switch ($this->sort_by)
		  {
		  	case 'natsort':
									natsort($temporary_array);
			break;
			case 'natcasesort':
									natcasesort($temporary_array);
			break;
			case 'sort':
				if($this->flags) 	sort($temporary_array,$this->flags);
				else				sort($temporary_array);
			break;
			case 'rsort':
				if($this->flags) 	rsort($temporary_array,$this->flags);
				else				rsort($temporary_array);
			break;
			case 'arsort':
				if($this->flags) 	arsort($temporary_array,$this->flags);
				else				arsort($temporary_array);
			break;
			case 'asort':
				if($this->flags) 	asort($temporary_array,$this->flags);
				else				asort($temporary_array);
			break;
			case 'krsort':
				if($this->flags) 	krsort($temporary_array,$this->flags);
				else				krsort($temporary_array);
			break;
			case 'ksort':
				if($this->flags) 	ksort($temporary_array,$this->flags);
				else				ksort($temporary_array);
			break;		
			default:
				natcasesort($temporary_array);
			break;
		  }
		   // delete double values
		   $temporary_array = array_unique($temporary_array);
		   // walk through temporary array
		   foreach($temporary_array as $temporary_value)
		   {
			   // walk through original array
			   foreach($array as $original_key => $original_value)
			   {
				   // and search for entries having the right value
				   if($temporary_value == $original_value[$sort_by])
				   {
					   // save in new array
					   $new_array[$original_key] = $original_value;
				   }
			   }
		   }
		   // update original array
		   $array = $new_array;
	   }
	   if($this->reverse)	return array_reverse($array);
	   else					return $array;			
	}
}
?>
Return current item: EZ Framework