Whenever you're wanting to sort something in some nontrvial fashion, the usort() function is often the way to go.
To use it, you create a function that, when given two elements of the array, will say whether the first is "before" or "after" the other - for whatever meaning of "before" and "after" you specify.
function compare_entries($a,$b)
{
/* $a and $b are two entries from the array being sorted.
If $a is "after" than $b, return -1;
if $a is "before" than $b, return 1;
If $a and $b are judged to be the same, return 0.
*/
}
For example, if $array is a list of arrays with keys 'foo', 'bar', 'baz' and 'wibble', and I want to sort them according to 'baz' and then 'wibble' respectively:
function compare_entries($a,$b)
{ if($a['baz']>$b['baz'])
return -1;
if($a['baz']<$b['baz'])
return 1;
// $a and $b have the same 'baz' value.
// Sort among these by 'wibble'.
if($a['wibble']>$b['wibble'])
return -1;
if($a['wibble']<$b['wibble'])
return 1;
// As far as I'm concerned, $a and $b are equivalent.
return 0;
}
Using this comparison function is simplicity itself. To sort $array using this function I go
usort($array,'compare_entries');
I hope that's of some help; once you've figured out exactly what it would mean for your array to be sorted, you should be able to encapsulate it in a comparison function and use it in usort(). For further detail, look up usort() in the manual.