First I am going to demonstrate the use of the php function usort(), and then show an example of how to organize your sorting features. There isn’t much to know about the function but it’s good to practice using it since sorting functions can get pretty complex when needed.

//A function must be defined and it must have 2 arguments
function sort_by_suffix($a, $b){

	$ax = intval(substr($a, 5));

	$bx = intval(substr($b, 5));

	if ($ax == $bx) return 0;

	elseif($a < $b) return -1;

	else return 1;

}

//an array of made up codes with a suffix at the end of each one
$codes = array("G45G-5", "G15G-2", "G11G-1", "G33G-3");

//usort modifies the array rather than returning it.
usort($codes, sort_by_suffix);

That was an example of sorting by a portion of a string. Whatever part of the string after 5 characters that could be made into a number was used as a sortable value. If it couldn’t find sortable values the order would be unpredictable. If the suffixes were all the same it would also be that way.

This next one will be a bit more tricky.

function sort_by_custom(){
	$numargs = func_num_args();
	$args = func_get_args();
	$a = $args[0];
	$b = $args[1];
	for($i = 2; $i < $numargs; $i++){
		$k = $args[$i]['key'];
		$p = $args[$i]['pol'];
		if($args[$i]['key']['type'] == 'numeric'){
			if ($a[$k] == $b[$k]) continue;
			elseif($a[$k] < $b[$k]) return -1 * $p;
			else return 1 * $p;
		}else{
			$s = strcmp($a[$k], $b[$k]);
			if($s == 0) continue;
			else return $s * $p;
		}
	}
	return 0;
}
function sort_by_time_name($a, $b){
	$time = array('key'=>'time', 'pol'=>1, 'type'=>'numeric');
	$name = array('key'=>'name', 'pol'=>1, 'type'=>'alphabet');
	return sort_by_custom($a, $b, $time, $name);
}
function sort_by_name_timedesc($a, $b){
	$name = array('key'=>'name', 'pol'=>1, 'type'=>'alphabet');
	$time = array('key'=>'time', 'pol'=>-1, 'type'=>'numeric');
	return sort_by_custom($a, $b, $name, $time);
}
$peo = array(
	array('name' => 'Joe', 'time' => 150),
	array('name' => 'Jon', 'time' => 150),
	array('name' => 'Jon', 'time' => 190),
	array('name' => 'Bob', 'time' => 750),
	array('name' => 'Karen', 'time' => 150),
	array('name' => 'Sal', 'time' => 190)
);
usort($peo, sort_by_time_name);
print_r($peo);
usort($peo, sort_by_name_timedesc);
print_r($peo);

sort_by_custom() shows no args but can accept any number of arguments. It compares $a and $b based on information in those arguments. sort_by_time_name() supplies a and b to sort_by_custom() and also tells it how to sort with the variables $name and $time. sort_by_name_timedesc() does that also, but the information is different. $peo is an example of something that might come up in a script either from user input or from a database. Let’s say it is a list of people and the time it takes them to drink a cup of coffee (in seconds). We want to sort our list by time. If times are equal, like 150 | 150, make it alphabetical by names. usort($peo, sort_by_time_name) does that. usort($peo, sort_by_name_timedesc) on the other hand sorts by name first, but if names are the same, and they are for ‘Jon’, then by time descending. So the pol value in the arrays in sort_by_name_timedesc control if it is positive or negative order. Using the principals here we can adapt usort to almost any type of sorting behavior we could possibly need.