Created
August 7, 2013 03:21
-
-
Save sofish/6170911 to your computer and use it in GitHub Desktop.
safari 不能用 true / false 来做排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// firefox 和 chrome 是 [1024, 6, 5, 3, 2, 1] | |
// safari 中顺序没变 | |
[1,3,2,5,6,1024].sort(function(a, b) { | |
return b > a; | |
}); | |
// 在各中浏览器工作一致的方法 | |
// 用正负和零来排序,而不是 true/false | |
[1,3,2,5,6,1024].sort(function(a, b) { | |
return b - a; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Safari 并没有把 true / false 转换成数字,根据 ES 的定义,需要函数返回 negative value、zero 和 positive value,所以 Safari 也没做错 http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.11