Skip to content

Commit

Permalink
#4
Browse files Browse the repository at this point in the history
  • Loading branch information
zenopopovici committed Apr 6, 2017
1 parent 9d72906 commit bdaaa5c
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: post

title: Sorting strings with accented characters
tip-number: 04
tip-username: loverajoel
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Javascript has a native method **sort** that allows sorting arrays. Doing a simple `array.sort()` will treat each array entry as a string and sort it alphabetically. But when you try order an array of non ASCII characters you will obtain a strange result.
tip-writer-support: https://www.coinbase.com/loverajoel
Expand Down Expand Up @@ -68,4 +68,9 @@ Fortunately, there are two ways to overcome this behavior [localeCompare](https:
- For each method you can customize the location.
- According to [Firefox](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare#Performance) Intl.Collator is faster when comparing large numbers of strings.

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

## Playground
<div>
<a class="jsbin-embed" href="https://jsbin.com/bazume/embed?js,console">JSTips.co - Sorting strings with accented characters on jsbin.com</a><script src="https://static.jsbin.com/js/embed.min.js?3.39.12"></script>
</div>

0 comments on commit bdaaa5c

Please sign in to comment.