I know that you can use the 'localeCompare' method to sort an array with your own localization as follows:
aArray.sort(function(a,b){return a.localeCompare(b)});
However, I believe that most (almost all) visitors to my Swedish site understand Swedish and, therefore, they expect Swedish sorting no matter what “locale” it currently uses.
(I think that some people will now be tempted to argue about this assumption, but I really think that there will be more people who understand Swedish but currently use a different language than people who do not know about the Swedish alphabet that they expected so that “å” and “ä” will be sorted equivalent to “a” and “ö”, sorted equivalent to “o.” I just want the last three three Swedish letters “ääö” to always be sorted in that order, regardless of browser or operating systems, regardless of what the "locale" used by the "localeCompare" method defines)
One solution could be the "locale" enforcement software used when calling the "localeCompare" method, but as I understand it, this is not possible. (although if it’s really possible, then how do you change the language?)
This sorting problem should be common enough for all javascript programmers, but still I could not find any open source library with a comparison method for Swedish strings. Does anyone know of such a library (implemented with good performance when using the sorting method) for reuse, and not for its implementation?
source
share