$arr = array (
'01 file.txt',
'1 file.txt',
'02 file.txt',
'2 file.txt',
'10 file.txt',
'12 file.txt',
);
$arr = array (
'01 file.txt',
'02 file.txt',
'1 file.txt',
'2 file.txt',
'10 file.txt',
'12 file.txt',
);
$arr = [
'002 file.txt',
'01 file .txt',
'12 file.txt',
'02 file.txt',
'001 file.txt',
'0002 file.txt',
'0010 file.txt',
'2 file.txt',
'1 file.txt',
'10 file.txt',
];
$val = static fn(string $v):string => substr($v, 0, strpos($v, ' '));
$zrs = static fn(string $v):int => strlen($v) - (int)(log10((int)$v) + 1);
usort($arr, function ($a, $b) use ($val, $zrs) {
$av = $val($a);
$bv = $val($b);
if ($zrs($av) == $zrs($bv)) {
return (int)$av > (int)$bv;
}
return $zrs($av) < $zrs($bv);
});
/*
Array
(
[0] => 0002 file.txt
[1] => 001 file.txt
[2] => 002 file.txt
[3] => 0010 file.txt
[4] => 01 file .txt
[5] => 02 file.txt
[6] => 1 file.txt
[7] => 2 file.txt
[8] => 10 file.txt
[9] => 12 file.txt
)
*/