Question

I'm build a site that is not in English, but some of the data may contain English. When I sort lists, for example:

sort($tags, SORT_LOCALE_STRING);

English strings always come first (because Latin letters have the lowest ordinals in Unicode), even though my site is primarily for non-English speakers.

I would like to have a sort function that sorts the strings lexicographically in the same way as the standard PHP sort, but always puts English strings after all the non-English strings. Any ideas?

Was it helpful?

Solution

Ok, this is cheating, but eventually I did this:

$tags = getSomeTags();
$tags_eng = $tags_lang = [];
foreach ($tags as $tag => $value) {
    if ($tag[0] <= 'z') $tags_eng[$tag] = $value;
    else $tags_lang[$tag] = $value;
}
ksort($tags_eng); ksort($tags_lang);
$tags = $tags_lang + $tags_eng;

Array "union" operation maintains the order of elements, so that works well.

Please note that its important not to use array_merge() for concatenating the arrays at the end, because if you have string keys that look like a number (compare "9" with 9), then array_merge() will think its an "indexed item" and will do weird stuff with it (resetting the key to the earliest available index).

I'd love it if someone can come up with a better method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top