不同浏览器中的Array.sort稳定性

时间:2013-05-10 09:39:15

标签: javascript

Array.sort Sorting Stability in Different Browsers

这是一个老问题,我认为如果我们在这里收集最新数据会有所帮助。 请点击这个小提琴

http://jsfiddle.net/Wrt9R/

并分享您的结果。

小提琴代码:

a = []
for(var i = 0; i < 1000; i++) {
    a.push({'key':100 + Math.round(Math.random() * 100), 'val': i + 1000 })
}

a.sort(function(x, y) { return x.key - y.key })

b = []
for(var i = 0; i < 1000; i++) {
    b.push(a[i].key * 10000 + a[i].val);
}

c = b.slice(0)
b.sort()
stable = (b.join() === c.join())

document.body.innerHTML = navigator.userAgent.toString() + "<br>" + (stable ? "stable": "UNSTABLE")

3 个答案:

答案 0 :(得分:8)

Stability     Browser             OS                  full UA string

unstable    Safari 5.3      OS X Lion (10.7.5)   Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.29.13 (KHTML, like Gecko) Version/6.0.4 Safari/536.29.13
stable      Firefox 15.0.1  OS X Lion            Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20100101 Firefox/15.0.1
unstable    Chrome 21       OS X Lion (10.7.5)   Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1
stable      Firefox 16      Win 7                Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
unstable    Chrome 26       Win 7 x64            Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
unstable    IE 9.0          Win 7 x64            Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E)
stable      Firefox 20      Win 7 x64            Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
unstable    IE 10           Win 7                Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3; .NET4.0C; .NET4.0E)
unstable    Chrome 26       Win 7                Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
stable      Firefox 20      Win 8 x64            Mozilla/5.0 (Windows NT 6.2; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
unstable    IE 10           Win 8 x64            Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Tablet PC 2.0)
unstable    Chrome 26       Win 8 x64            Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
unstable    Opera 12.15     Win 8 x64            Opera/9.80 (Windows NT 6.2; WOW64) Presto/2.12.388 Version/12.15
stable      Firefox 20      Ubuntu               Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0
unstable    Chrome 26                            Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31
unstable    Chrome 20       Win 7 x64            Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
unstable    Chrome 28       Win 8 x64            Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36

答案 1 :(得分:0)

Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 536.11(KHTML,与Gecko一样)Chrome / 20.0.1132.57 Safari / 536.11 不稳定

答案 2 :(得分:0)

在IE10 Win7 x64上:

Mozilla / 5.0(兼容; MSIE 10.0; Windows NT 6.1; WOW64; Trident / 6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; HPNTDF; .NET4.0C) 不稳定