How to sort an array of strings with non latin letters?

Maciej Trzciński
Oct 29, 2018 · 1 min read
Photo by Ryoji Iwata on Unsplash

JavaScript has a native sort method, you can do it with array.sort(), it will sort the array alphabetically. Also, you can provide your custom sorting functionality.

But if you want to order an array of non-ASCII character like this [‘ą’, ‘ę’, ‘ó’, ‘ż’, ‘ź’, ‘e’], you will get a [“e”, “ó”, “ą”, “ę”, “ź”, “ż”]. That happens because sort function works only with the English alphabet.

Fortunately, there are two ways to overcome this behavior localeCompare and Intl.Collator provided by ECMAScript Internationalization API.

Using localeCompare()

Using Intl.Collator()

So when you are working with arrays of strings in a language other than English, remember to use this method to avoid unexpected sorting, but I recommend using this method always.

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Maciej Trzciński

Written by

Quick Code
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade