Really interesting question! Each character has a Unicode value. Most sorting is done through that. Since Latin letters are in the ASCII range, those names always come up first. PHP's asort
function will take Unicode into consideration. Here is an input to consider:
$input = [
[
"firstName" => "一",
"lastName" => "風"
],
[
"firstName" => "이",
"lastName" => "정윤"
],
[
"firstName" => "Mari",
"lastName" => "M"
],
[
"firstName" => "三",
"lastName" => "火"
],
];
Let's summarize what I expect to see, assuming we sort by first name:
- Latin name first (Mari M)
- Hanzi/kanji/hangeul names next. I don't know what the values of these names are, so we have to find out.
Let's convert the first character of the first names to something numeric. Again, we are using Unicode for this conversion:
- 一 is 0x4E00
- 이 is 0xC774
- M is 0x004D
- 三 is 0x4E09
As such, I expect to see, in order:
Here is my code, using asort
:
$nameByFirst = [];
foreach( $input as $i )
{
$nameByFirst[] = $i["firstName"]." ".$i["lastName"];
}
asort($nameByFirst);
And my printing method:
$i = 1;
foreach( $nameByFirst as $name )
{
echo $i.'. '.$name."<br>";
$i++;
}
And my output:
- Mari M
- 一 風
- 三 火
- 이 정윤
My results, as you can see above, are in order. Latin first, then hanzi/kanji, then hangeul. Unicode is the closest I believe we can get to an easy sort, so I like to go by that. I'm not 100% sure on how Unicode assigned values to hanzi/kanji/hangeul, but I'm willing to trust the order they provided, especially because of how simple it is.