Created
January 20, 2024 13:22
-
-
Save alpenzoo/548aebb9816dfe87aac813adb3734409 to your computer and use it in GitHub Desktop.
custom collate like sort function PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compare_by_alphabet($str1, $str2) { | |
$alphabet = "AaÀàÁáÂâÅåÃãÄ䯿BbCcÇçDdÐðEeÈèÉéÊêËëFfGgHhIiÌìÍíÎîÏïJjKkLlMmNnÑñOoÒòÓóÔôÕõÖöØøPpQqRrSsߊšTtUuÙùÚúÛûÜüVvWwXxYyŸÿÝýZzŽžÞþ0123456789"; | |
$l1 = mb_strlen($str1); | |
$l2 = mb_strlen($str2); | |
$c = min($l1, $l2); | |
for ($i = 0; $i < $c; $i++) | |
{ | |
$s1 = mb_substr($str1, $i, 1); | |
$s2 = mb_substr($str2, $i, 1); | |
if ($s1===$s2) continue; | |
$i1 = mb_strpos($alphabet, $s1); | |
if ($i1===false) continue; | |
$i2 = mb_strpos($alphabet, $s2); | |
if ($i2===false) continue; | |
if ($i2===$i1) continue; | |
if ($i1 < $i2) return -1; | |
else return 1; | |
} | |
if ($l1 < $l2) return -1; | |
elseif ($l1 > $l2) return 1; | |
return 0; | |
} | |
$names=["Schön","Åsberg","Zierer","Ås","Žižek","Schon","Asber"]; | |
usort($names, 'compare_by_alphabet'); | |
var_dump($names); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment