简体   繁体   English

JavaScript IE 9:自定义排序功能

[英]JavaScript IE 9: Custom sort function

In IE 9, if I type this in the console: 在IE 9中,如果我在控制台中键入以下内容:

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

The resulting array is: [1, 4, 2, 3] . 结果数组为: [1, 4, 2, 3]

If I do this in FF/Chrome, I get it, reverse-sorted: [4, 3, 2, 1] . 如果我在FF / Chrome中执行此操作,则会得到反向排序: [4, 3, 2, 1] 4,3,2,1 [4, 3, 2, 1]

How come this doesn't work in IE? 为什么这在IE中不起作用?

EDIT: Is there a function out there that abstracts these browser differences? 编辑:那里有一个抽象这些浏览器差异的功能吗? In other words, is there a function I can pass function(a, b) { return a < b; 换句话说,有没有我可以传递的函数function(a,b){return a <b; } in and get the same result in all browsers? }并在所有浏览器中获得相同的结果? Any open-source stuff? 有开源的东西吗?

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). 也许是因为该函数应该返回-1 ,如果a是小, 0 ,如果他们是平等的,和1如果a较大(或反之,如果你想颠倒顺序)。 Edit: 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). 编辑:实际上,它必须为零,正数或负数(如@pimvdb所指出的,这就是我在以下示例中使用的东西)。 Your function will never return -1 though and that might create problems. 但是,您的函数将永远不会返回-1 ,这可能会导致问题。

Consider 1 and 3 . 考虑13 Your function returns 1 for 1 < 3 , which is ok, but returns 0 for 3 < 1 . 您的函数对于1 < 3返回1 ,这是可以的,但是对于3 < 1返回0 In one case the number are different, in the other you are saying that they are equal. 在一种情况下,数字不同,在另一种情况下,您说的是相等。

That FF/Chrome sort it in reverse order might be due to the sorting algorithm they are using. FF / Chrome以相反的顺序对其进行排序的原因可能是由于他们使用的排序算法。

Try: 尝试:

[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: 更新:为了证实这一点,我们可以看一下规范15.4.4.11 Array.prototype.sort(comparefn ),其中给出了必须由比较函数实现的属性:

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; 函数comparefn是一组值的一致的比较函数S如果以下所有条件都符合的集合中的所有值a,bc(可能是相同的值)的要求S :该表示法 <CF B表示comparefn (a,b) <0; a = CF b means comparefn(a,b) = 0 (of either sign); a = CF b表示comparefn(a,b) = 0(任一符号); and a > CF b means comparefn(a,b) > 0. a > CF b表示comparefn(a,b) > 0。

  • Calling comparefn(a,b) always returns the same value v when given a specific pair of values a and b as its two arguments. 给定特定的一对值ab作为其两个参数时调用comparefn(a,b)总是返回相同的值v。 Furthermore, Type( v ) is Number, and v is not NaN . 此外,Type( v )是Number, v不是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 . 请注意,这意味着对于给定的ab对,恰好 < CF ba = CF ba > CF b之一。
  • Calling comparefn(a,b) does not modify the this object. 调用comparefn(a,b)不会修改对象。
  • a = CF a (reflexivity) a = CF a (反射率)
  • If a = CF b , then b = CF a (symmetry) 如果a = CF b ,则b = CF a (对称)
  • If a = CF b and b = CF c , then a = CF c (transitivity of = CF ) 如果a = CF b并且b = CF c ,则a = CF c (传递性= CF
  • If a < CF b and b < CF c , then a < CF c (transitivity of < CF ) 如果a < CF bb < CF c ,则a < CF c (传递度< CF
  • If a > CF b and b > CF c , then a > CF c (transitivity of > CF ) 如果a > CF bb > CF c ,则a > CF c (传递性> CF

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. 注意:上述条件是必要的,并且足以确保comparefn将集合S划分为等价类,并且确保这些等价类完全有序。

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

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