Skip to content

Instantly share code, notes, and snippets.

@sofish
Created August 7, 2013 03:21
Show Gist options
  • Save sofish/6170911 to your computer and use it in GitHub Desktop.
Save sofish/6170911 to your computer and use it in GitHub Desktop.
safari 不能用 true / false 来做排序
// 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;
});
@chrisyip
Copy link

chrisyip commented Aug 7, 2013

Safari 并没有把 true / false 转换成数字,根据 ES 的定义,需要函数返回 negative value、zero 和 positive value,所以 Safari 也没做错 http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment