简体   繁体   English

是否有任何javascript库与字母表的排序方法的实现,如瑞典语?

[英]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: 我知道您可以使用方法' localeCompare '来排序具有您自己的本地化的数组,如下所示:

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 ") (我认为现在有些人会想要争论这个假设,但我真的认为会有更多的人理解瑞典语,但目前使用的是其他语言环境而不是那些不了解瑞典语字母的人我希望“å”和“ä”的排序等同于“a”和“ö”等同于“o”。我真的希望最后三个瑞典字母“åäö” 始终按顺序排序, 无论如何浏览器或操作系统,无论它是什么定义了“ 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. 一种解决方案可能是以编程方式强制执行“localeCompare”方法时使用的“语言环境”,但据我所知,这是不可能的。 (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. 这个排序问题应该对所有瑞典javascript程序员来说都相当普遍,但我仍然无法找到任何开源库,其中包含瑞典字符串比较方法的实现。 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 ? 有没有人知道这样的库(与sort方法一起使用时性能良好)重用而不是自己尝试实现它?

JavaScript Internationalization API offers exactly what you need. JavaScript Internationalization API提供您所需要的。 There are some usage examples on page I referenced. 第I页引用了一些用法示例。 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? ) @Richard Marr(评论是否有任何javascript库,包括瑞典等字母表的排序方法实现?

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. 好吧,我不确定这会非常困难,但对我来说可能不是很容易使它像你可能用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. 感觉就像试图重新发明轮子一样,擅长javascript的人可能已经做得比我做得更好。 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. 在我看来,你期望a和b成为角色。 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. 确实可以使用这样的位置结构,但由于sort方法的函数参数接收两个任意字符串而不是只有一个字符的字符串,所以我猜两个字符串中的每个字符(直到第一个差异)都必须迭代并且与职位结构相比。 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? ) @Pawel Dyda(评论是否有任何javascript库,包括瑞典语等字母表的排序方法实现?

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: 不,不仅仅是JS,但我找到了jQuery tablesorter,我认为这是一个很好的,但是当我找到以下代码时遇到了排序问题:

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

(which is documented at "sortLocaleCompare" in the page http://mottie.github.com/tablesorter/docs/ ) (在http://mottie.github.com/tablesorter/docs/页面的“sortLocaleCompare”中记录)

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). 事实上,我打算在服务器上实现排序,也支持使用禁用javascript的客户端,但对于使用javascript的客户,我认为我可以使用tablesorter而无需再次请求Web服务器获取相同的结果集(但排序方式不同) )。 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. 至少为结果集实现它非常简单,不需要分页,即当所有行都适合一个页面时,你可以为jQuery tablesorter生成“ serverSideSorting:false ”,然后它会自动在Web中对它进行排序浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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