Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any javascript library with implementations of sort methods for alphabets such as swedish?

I am aware of the fact that you can use the method 'localeCompare' to sort an array with your own localization like this:

aArray.sort(function(a,b){return a.localeCompare(b)});

However, I assume that most (virtually all) visitors of my swedish site do understand swedish and therefore they will expect swedish sorting regardless which "locale" he/she currently is using.

(I think that some people now would be tempted to argue about this assumption, but I really think that there will be a greater number of people who are understanding swedish but currently using another locale than there would be people not knowing about the swedish alphabet who would expect "å" and "ä" to be sorted equivalent with "a" and "ö" sorted equivalent with "o". I just really want the three last three swedish letters "åäö" to always be sorted in that order, regardless of the browser or operating systems, whatever it is that defines the "locale" used by the method "localeCompare")

One solution might have been to programmatically enforce the "locale" used when the method "localeCompare" is invoked, but as far as I understand this is not possible. (though if it actually is possible, then how do you change the locale ?)

This sorting problem should be fairly common to all swedish javascript programmers but still I have not been able to find any open source library with an implementation of a comparison method for swedish strings. Does anyone here know about such a library (implemented with good performance when used with the sort method) to reuse instead of trying to implement it yourself ?

like image 986
user310457 Avatar asked Nov 12 '22 12:11

user310457


1 Answers

JavaScript Internationalization API offers exactly what you need. There are some usage examples on page I referenced. Unfortunately, it is not widely supported by browsers yet.

like image 103
skalee Avatar answered Nov 15 '22 06:11

skalee