Reputation:
I'm trying to sort an array using natsort/natcasesort. But I'm having trouble with non-English (In Turkish) characters. This is the only option that works for me at the moment. How can I overcome this problem? For example, the array looks like this:
$texts= array("A","Ü","Ç","Ş","Ğ");
natcasesort($texts);
echo '<pre>'; print_r($files); echo '</pre>';
Output:
Array
(
[0] => A
[2] => Ç
[1] => Ü
[4] => Ğ
[3] => Ş
)
$all_characters = [ "ğ", "Ğ", "ç", "Ç", "ş", "Ş", "ü", "Ü", "ö", "Ö", "ı", "İ" ];
$alphabet_all = "AaBbCcÇçDdEeFfGgĞğHhIıİiJjKkLlMmNnOoÖöPpQqRrSsŞşTtUuÜüVvWwXxYyZz";
$small_letters = array("İ","I","Ş","Ğ","Ö","Ü","Ç");
$capital_letters = array("i","ı","ş","ğ","ö","ü","ç");
How should it be ?
A, Ç, Ğ, Ş, Ü
Upvotes: 1
Views: 177
Reputation: 7693
Natsort is not suitable for language-specific sorting. That's what the Collator class is for.
$all_characters = [ "ğ", "Ğ", "ç", "Ç", "ş", "Ş", "ü", "Ü", "ö", "Ö", "ı", "İ" ];
$collator = new Collator('tr_TR');
//The following line is only required if natsort is desired
$collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON);
$collator->sort($all_characters);
echo implode(', ',$all_characters);
//ç, Ç, ğ, Ğ, ı, İ, ö, Ö, ş, Ş, ü, Ü
Upvotes: 3