2014-06-24, 10:28:18
you should use a natcasesort sort so that your mix of alpha and numerics sort properly.
1 before 10 etc, subval in core has this as default.
Tthis does not preserve keys however unless you edit
$c[] = $a[$key]; to $c[$key] = $a[$key]
which will be fixed in 3.4
1 before 10 etc, subval in core has this as default.
PHP Code:
/**
* Sub-Array Sort
*
* Sorts the passed array by a subkey
*
* @since 1.0
*
* @param array $a
* @param string $subkey Key within the array passed you want to sort by
* @param string $order - order 'asc' ascending or 'desc' descending
* @param bool $natural - sort using a "natural order" algorithm
* @return array
*/
function subval_sort($a,$subkey, $order='asc',$natural = true) {
if (count($a) != 0 || (!empty($a))) {
foreach($a as $k=>$v) {
if(isset($v[$subkey])) $b[$k] = lowercase($v[$subkey]);
}
if(!isset($b)) return $a;
if($natural){
natsort($b);
if($order=='desc') $b = array_reverse($b,true);
}
else {
($order=='asc')? asort($b) : arsort($b);
}
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
}
Tthis does not preserve keys however unless you edit
$c[] = $a[$key]; to $c[$key] = $a[$key]
which will be fixed in 3.4