JavaScript IE 9:自定义排序功能 [英] JavaScript IE 9: Custom sort function

查看:78
本文介绍了JavaScript IE 9:自定义排序功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IE 9中,如果我在控制台中键入以下内容:

In IE 9, if I type this in the console:

[1, 4, 2, 3].sort(function (a, b) { return a < b; });

结果数组为:[1, 4, 2, 3].

如果我在FF/Chrome中执行此操作,则会得到反向排序的[4, 3, 2, 1].

If I do this in FF/Chrome, I get it, reverse-sorted: [4, 3, 2, 1].

这怎么在IE中不起作用?

How come this doesn't work in IE?

是否有一个抽象这些浏览器差异的函数?换句话说,有没有我可以通过的函数function(a,b){return a< b; }并在所有浏览器中获得相同的结果?有开源的东西吗?

推荐答案

也许是因为如果a较小,则函数应返回-1;如果相等,则返回0;如果1 >较大(反之亦然). 实际上,它必须为零,为正数或负数(如@pimvdb所指出的,这就是我在下面的示例中所使用的).但是,您的函数将永远不会返回-1,这可能会导致问题.

Maybe because the function is supposed to return -1 if a is smaller, 0 if they are equal, and 1 if a is larger (or vice versa if you want reverse order). In fact, it must be zero, a positive or negative number (as @pimvdb pointed out, and that's what I make use of in the example below). Your function will never return -1 though and that might create problems.

请考虑13.您的函数为1 < 3返回1,可以,但是为3 < 1返回0.在一种情况下,数字不同,而在另一种情况下,您说的是相等.

Consider 1 and 3. Your function returns 1 for 1 < 3, which is ok, but returns 0 for 3 < 1. In one case the number are different, in the other you are saying that they are equal.

FF/Chrome以相反的顺序对其进行排序可能是由于他们使用的排序算法所致.

That FF/Chrome sort it in reverse order might be due to the sorting algorithm they are using.

尝试:

[1, 4, 2, 3].sort(function (a, b) { return b - a; });

更新:要证实这一点,我们可以看一下规范

Update: To substantiate this, we can have a look at the specification, Section 15.4.4.11 Array.prototype.sort(comparefn), where the properties are given which have to be fulfilled by a comparison function:

如果所有值 a b都满足以下所有要求,则函数 comparefn 是针对一组值S的一致比较函数. c (可能是相同的值)在集合S中:符号 a < CF b 表示 comparefn(a,b)< 0; a = CF b 表示 comparefn(a,b) = 0(任一符号);和 a > CF b 表示 comparefn(a,b)> 0.

A function comparefn is a consistent comparison function for a set of values S if all of the requirements below are met for all values a, b, and c (possibly the same value) in the set S: The notation a <CF b means comparefn(a,b) < 0; a =CF b means comparefn(a,b) = 0 (of either sign); and a >CF b means comparefn(a,b) > 0.

    当给定特定的一对值 a b 时,调用
  • comparefn(a,b)总是返回相同的值v论点.此外,类型( v )是数字,而 v 不是 NaN .请注意,这暗示着 a < CF b 中的一个, a = CF b a > CF b a b 对,em>都是正确的.
  • 调用 comparefn(a,b)不会修改 this 对象.
  • a = CF a (自反)
  • 如果 a = CF b ,则 b = CF a (对称)
  • 如果 a = CF b b = CF c ,然后是 a = CF c (传递性= CF )
  • 如果 a < CF b b < CF c ,然后是 a < CF c (可传递性<< CF )
  • 如果 a > CF b b > CF c ,然后是 a > CF c (> CF 的传递性)
  • Calling comparefn(a,b) always returns the same value v when given a specific pair of values a and b as its two arguments. Furthermore, Type(v) is Number, and v is not NaN. Note that this implies that exactly one of a <CF b, a =CF b, and a >CF b will be true for a given pair of a and b.
  • Calling comparefn(a,b) does not modify the this object.
  • a =CF a (reflexivity)
  • If a =CF b, then b =CF a (symmetry)
  • If a =CF b and b =CF c, then a =CF c (transitivity of =CF)
  • If a <CF b and b <CF c, then a <CF c (transitivity of <CF)
  • If a >CF b and b >CF c, then a >CF c (transitivity of >CF)

注意:上述条件对于确保 comparefn 将集合S划分为等价类并确保这些等价类完全有序是必要的.

NOTE The above conditions are necessary and sufficient to ensure that comparefn divides the set S into equivalence classes and that these equivalence classes are totally ordered.

这篇关于JavaScript IE 9:自定义排序功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆