我有一个这样的数组:
[1, Stopped]
[2, Waiting]
[3, Finished]
[4, Stopped]
[5, Running]
数字是程序的ID,文本是程序的状态。我需要根据下一个顺序对这个数组进行排序:
['Error','Halted','Blocked','Finished','Waiting to Start','Waiting','Stopping','Running','Idle','Stopped','Opened','Ready'];
有谁能告诉我如何使用预定义的顺序对数组进行排序?
除IE8外,它适用于所有不同的浏览器。任何人都可以告诉我如何在IE8中对它进行排序
答案 0 :(得分:2)
您可以使用:
if (typeof Array.prototype.indexOf !== 'function') {
Array.prototype.indexOf = function (el) {
for (var i = 0; i < this.length; i += 1) {
if (this[i] === el) return i;
}
return -1;
}
}
var a = [[1, 'Stopped'],
[2, 'Waiting'],
[3, 'Finished'],
[4, 'Stopped'],
[5, 'Running']];
var order = ['Error','Halted','Blocked','Finished','Waiting to Start','Waiting','Stopping','Running','Idle','Stopped','Opened','Ready'];
a.sort(function (a, b) {
return order.indexOf(a[1]) - order.indexOf(b[1]);
});