I'm going to assume you mean strings starting with a letter should appear before all other strings and all strings should otherwise be sorted in the standard order.
You use usort()
and define a custom function for the ordering and ctype_alpha()
to determine if something is a letter or not.
$arrs = Array("ABC_efg", "@@zzAG", "@$abc", "ABC_abc");
usort($arrs, 'order_alpha_first');
function order_alpha_first($a, $b) {
$lenA = strlen($a);
$lenB = strlen($b);
$len = min($lenA, $lenB);
$i = 0;
while ($a[$i] == $b[$i] && $i < $len) {
$i++;
}
if ($i == $len) {
if ($lenA == $lenB) {
return 0; // they're the same
} else {
return $lenA < $lenB ? -1 : 1;
}
} else if (ctype_alpha($a[$i])) {
return ctype_alpha($b[$i]) ? strcmp($a[$i], $b[$i]) : -1;
} else {
return ctype_alpha($b[$i]) ? 1 : strcmp($a[$i], $b[$i]);
}
}
Output:
Array
(
[0] => ABC_abc
[1] => ABC_efg
[2] => @$abc
[3] => @@zzAG
)