PHP – Class Array Sorter

This PHP Class can be used to sort multidimensional arrays.

It takes multidimensional array and sorts or an array of objects and sorts it by the value of a
given array entry or object variable.

The sorting entry name may include one more entry names, one per each dimension.

The sorting order may be configured also to be ascending or descending.

Code Example: Download Example
index.php
<?php
/**
* Example of sorting multidimensional array
*/

//test data
$ar = array();
$ar[0]['name'] = 'Terry';
$ar[0]['surname'] = 'Cook';
$ar[0]['addr']['street'] = 'ABBAY ROAD';
$ar[0]['addr']['no'] = '10';
$ar[0]['salary'] = 25000;

$ar[1]['name'] = 'Anna';
$ar[1]['surname'] = 'Smith';
$ar[1]['addr']['street'] = 'STREATHAM PLACE';
$ar[1]['addr']['no'] = '1';
$ar[1]['salary'] = 15000;

$ar[2]['name'] = 'John';
$ar[2]['surname'] = 'Doe';
$ar[2]['addr']['street'] = 'GAUNT STREET';
$ar[2]['addr']['no'] = '101';
$ar[2]['salary'] = 30000;

require('sorter.php');

//initialize sorter
$oSorter = new ArraySorter();
//set data to sort
$oSorter->setArray($ar);

echo "<pre>";
//sort by addr street ascending
print_r($oSorter->sort('addr.street', ArraySorter::DIRECTION_ASC));

echo '<hr />';
//sort by salary descending
print_r($oSorter->sort('salary', ArraySorter::DIRECTION_DESC));
?>
sorter.php
<?php
/**
* ArraySorter
*
* Copyright (c) 2011 Przemek Berezowski (przemek@otn.pl)
* All rights reserved.
*
* @category	  Library
* @package  	 ArraySorter
* @copyright     Copyright (c) 2011 Przemek Berezowski (przemek@otn.pl)
* @version       0.9
* @license       New BSD License
*/
class ArraySorter {

	const DIRECTION_ASC = 1;
	const DIRECTION_DESC = 2;

	/**
	 *
	 * @var array - array to sort
	 */
	private $array2sort;

	/**
	 *
	 * @var string - string to describe field position
	 */
	private	$sortField;
	private $direction = 1;

	/**
	 * Construct
	 * @param array $array2sort
	 */
	public function __construct($array2sort = array()) {
		$this->setArray($array2sort);
	}

	/**
	 * Setter to assign array to sort to a class
	 *
	 * @param unknown_type $array2sort
	 */
	public function setArray($array2sort) {
		$this->array2sort = $array2sort;
	}

	/**
	 *
	 * Sort method
	 * @param string $field - string to describe field position
	 * @param int $direction - based on class constants
	 * @return array - sorted array:
	 */
	public function sort($field, $direction) {
		$this->sortField = explode('.', $field);
		$this->direction = $direction;
		usort($this->array2sort, array($this, 'doSort'));
		return $this->array2sort;
	}

	/**
	 *
	 * Callback function to compare elements of sorting array
	 * @param mixed $a
	 * @param mixed $b
	 * @return int;
	 */
	protected function doSort($a, $b) {

		$cmp1 = $this->getSortField($a);
		$cmp2 = $this->getSortField($b);
		if ($cmp1 == $cmp2) {
			return 0;
		}

		if ($this->direction == self::DIRECTION_ASC) {
			return ($cmp1 < $cmp2) ? -1 : 1;
		} else {
			return ($cmp1 < $cmp2) ? 1 : -1;
		}
	}

	/**
	 * method to find value to campare
	 * @param mixed $sortElement
	 */
	protected function getSortField($sortElement) {

		$val = $sortElement;

		foreach($this->sortField as $key) {

			if (is_object($val) && isset($val->$key)) {
				$val = $val->$key;
			} elseif (isset($val[$key])) {
				$val = $val[$key];
			} else {
				break;
			}
		}
		return $val;
	}

}
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

1 Comment

  1. avatarVishal Gularia

    Thanks.

    sounds good

Leave a Comment

Your email address will not be published. Required fields are marked *