Write PHP in English sort
Such as:
A
Acb
Abc
Aca
Aaaa
Can be lined up as:
A,aaaa,abc,aca,acb
That is, a row in front, Z is the last, the first letter from A-Z, the second third is infinitely from A-Z order.
PHP English Sort
Share to:
------Solution--------------------
This demand is more alternative
$a = Array (
' A ',
' ACB ',
' ABC ',
' ACA ',
' AAAA ',
);
$m = max (Array_map (' strlen ', $a));
foreach ($a as $v) {
$c = substr ($v,-1);
$t [] = Str_pad ($v, $m, $c);
}
Array_multisort ($t, $a);
Print_r ($a);
Array
(
[0] = a
[1] = = AAAA
[2] = ABC
[3] = ACA
[4] = ACB
)
------Solution--------------------
References:
This demand is more alternative
$a = Array (
' A ',
' ACB ',
' ABC ',
' ACA ',
' AAAA ',
);
$m = max (Array_map (' strlen ', $a));
foreach ($a as $v) {
$c = substr ($v,-1);
$t [] = Str_pad ($v, $m, $c);
}
Array_multisort ($t, $a);
Print_r ($a);
Array
(
[0] = a
[1] = = AAAA
[2] = ABC
[3] = ACA
[4] = ACB
)
Why not use the sort row directly?
------Solution--------------------
System functions can do it.
$arr = Array (
' A ',
' ACB ',
' ABC ',
' ACA ',
' AAAA '
);
Sort ($arr);
Print_r ($arr);