We can try using usort() here with a custom comparator function:
$arr = array(...);
function getOrder($input) {
$order = 4;
if (preg_match("/^[a-z]/", $input)) {
$order = 1;
}
else if (preg_match("/^[A-Z]/", $input)) {
$order = 2;
}
else if (preg_match("/^[0-9]/", $input)) {
$order = 3;
}
else {
$order = 4;
}
return $order;
}
usort($arr, function($a, $b) {
$a_order = getOrder($a);
$b_order = getOrder($b);
if ($a_order == $b_order) {
return strcmp($a, $b);
}
if ($a_order < $b_order) {
return -1;
}
else {
return 1;
}
});
$print_r($arr);
This prints:
Array
(
[0] => a001.txt
[1] => b002.TXT
[2] => c003.txt
[3] => x004.txt
[4] => y005.TXT
[5] => z006.txt
[6] => A001.TXT
[7] => B002.TXT
[8] => C003.TXT
[9] => X004.TXT
[10] => Y005.txt
[11] => Z006.TXT
[12] => 0001.TXT
[13] => 1001.txt
[14] => 2001.TXT
)
The strategy here is to use the getOrder() function to determine whether a filename begin with a lowercase, uppercase, or number. For each of these cases, we assign a priority, increasing in that order. Then, in the lambda comparator function used with usort(), we compare the priorities of the two incoming filenames. Note that for the case where two filenames happen to start with the same type (e.g. both lowercase), we fall back to strcmp() to determine which comes first.
usort()and implement any comparation function You like.a001.txtanda001.TXTare two separate files with the same naming convention prior to the extension. 1) this would solve your issue and 2) it will alleviate more confusion in the future should two files be named the same thing with different cases on the extension. Also, just in case you are using uppercase and lowercase of the same extension to differentiate between files - Don't. This is also bad practice and a more thorough naming convention should be established.