I recently found out that PHP already supports Unicode Collation Algorithm through the intl extension :
$array = array ( 'al', 'be', 'Alpha', 'Beta', 'Álpha', 'Àlpha', 'Älpha', 'かたかな', 'img10.png', 'img12.png', 'img1.png', 'img2.png', ); if (extension_loaded('intl') === true) { collator_asort(collator_create('root'), $array); } Array ( [0] => al [2] => Alpha [4] => Álpha [5] => Àlpha [6] => Älpha [1] => be [3] => Beta [11] => img1.png [9] => img10.png [8] => img12.png [10] => img2.png [7] => かたかな )
As you can see, this seems to work just fine, even with mixed strings! The only drawback I have encountered so far is that there is no support for natural sorting , and I wonder what would be the best way to work around this so that I can combine the best of the two worlds.
I tried to specify the Collator::SORT_NUMERIC sort flag, but the result became more messy:
collator_asort(collator_create('root'), $array, Collator::SORT_NUMERIC); Array ( [8] => img12.png [7] => かたかな[9] => img10.png [10] => img2.png [11] => img1.png [6] => Älpha [5] => Àlpha [1] => be [2] => Alpha [3] => Beta [4] => Álpha [0] => al )
However, if I run the same test only with img*.png values, I get the perfect output:
Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )
Can anyone think about how to save Unicode sorting by adding natural sorting capabilities?
sorting php natural-sort unicode
Alix axel
source share