How to Sort an Array by Locale in PHP

As easy as it is to translate content within WordPress, there are  times in which some of the content in the database may be different than the original locale of the site.

Say, for example, you’re running a site that includes a list of names in a flavor German when your server (or even perhaps your site) is based on an English locale. The goal is to read the names into a collection – rather, an array – and then sort them alphabetically.

Obviously, different locales have different alphabets so what works in, say, the United States won’t necessarily work in Germany.

There are a couple of ways to go about handling this, but the easiest way that I’ve found is to use PHP’s Collator class.

Sort Array By Locale in PHP

When it comes to using certain classes that are provided by PHP, I’m always hesitant because the feature may not be compiled into the server’s installation of PHP, and if that’s the case, then we’re out of luck and have to find a different solution.

But in this case – and perhaps I’ve just been lucky – there haven’t been any issues when I’ve needed to leverage the Collator class in various projects on which I’ve worked.

What About setlocale?

Before I talk about anymore about the Collator, it’s worth mentioning that many – if not most – of the examples and tutorials on the web that talk about sorting data with a language in a different locale than that of what’s on the server (or in the application, if it has a unique one specified) is to use PHP’s setlocale function.

The way the function works is simple:

  • You specify a category that will affect the category of sorting functions. I typically use LC_COLLATE and, if I use something else, then it’s LC_ALL.
  • You specify the locale for the language that you want to convert.

The function will then return the locale that was originally set so that you can define it later, and set a new locale based on what you’ve specified.

For example, check out the following gist:

Easy enough to understand, isn’t it?

The thing is, I’ve had really mixed results with this. Most notably, with certain locales, it does not appear to order the alphanumeric characters based on their true-to-life ordering so when an attempt to sort the data is made, it doesn’t work correctly.

The Collator Class

This is where the Collator class comes in really handy. If you’re familiar with the syntax above, then it’s really easy to use.

But first, according to the PHP manual, the Collator class is defined as follows:

Provides string comparison capability with support for appropriate locale-sensitive sort orderings.

Sounds exactly like what’s needed, doesn’t it?

So let’s say I have an array of last names and I want to sort them alphabetically using the German locale. To use the Collator, I would do this:

Not much different is it? Of course, the main difference is that this class – at least in my experience – appears to handle international alphabets and their ordering better than setlocale.

I’m not above saying that I’ve missed something when using setlocale, but I’ve had more success with the Collator than the alternative, so I thought it would be worth sharing.

Oh! And About Locales…

Additionally, when it comes to setting locales regardless of the method that you’re using, you need to make sure that you’re using the proper locale string.

For example, the string for generalized English is en but if you wanted to use English as its treated in, say, Great Britain, then you’d pass en_GB for example.

Anyway, here’s a useful page on MSDN that I’ve found for referencing language strings when defining locale. I’m sure there are other good resources available (and I welcome them in the comments), but this page and the Collator have yet to disappoint when needing to sort this kind of information.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.