简体   繁体   中英

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 ?

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.

@Richard Marr (the comment Is there any javascript library with implementations of sort methods for alphabets such as swedish? )

Well I am not sure it would be very difficult, but for me probably not very easy to make it as efficient as you possibly can do with javascript. It feels like trying to reinvent the wheel, and someone who is good at javascript have probably already done it better than I could do. Considering how many open source libraries that exist for just about anything I would be a bit surprised to have to realize that this thing does not exist yet.

Regarding your code snippet with

'sort(function(a,b){ return position[a] - position[b]; })' 

it seems to me as you expect a and b to be characters. For example I suppose the following kind of structure is what you had in mind:

var position = {
// e.g. essentially the codes here: http://en.wikipedia.org/wiki/Multinational_Character_Set
'A': 65,
'B': 66,
//...
// the three swedish characters:
//'Ä': 196, 
//'Å: 197,
//'Ö': 214,
// The above are the correct values according to the codes, but the relative ordering of Å and Ä is not correct for swedish so therefore I switch the values below:
'Å': 196, 
'Ä': 197,
'Ö': 214
};

Indeed such a position structure could be used, but since the function parameter for the sort method receives two arbitrary strings rather than strings with only one character, then I guess each character (until the first difference) in both strings would have to be iterated and compared with the position structure. I was hoping that there is a better (more efficient) way of doing it ...

@Pawel Dyda (comment Is there any javascript library with implementations of sort methods for alphabets such as swedish? )

No, not JS-only, but I have found the jQuery tablesorter, which I think was a nice one, but then ran into the sorting problem when I found the following code:

$('table').tablesorter({ 
  textSorter: function(a,b) { 
    return a.localeCompare(b); 
  } 
});

(which is documented at "sortLocaleCompare" in the page http://mottie.github.com/tablesorter/docs/ )

Indeed I intend to implement the sorting at the server, to also support clients with disabled javascript, but for clients who are using javascript I thought I could use the tablesorter without the need to request the web server again for the same resultset (but sorted differently). At least it is very trivial to implement it for resultsets small enough for not needing paging, ie when all rows fits into one page then you can just generate " serverSideSorting: false " for the jQuery tablesorter and then it automatically will sort it within the web browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM