我有一个像这样的数组:
[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中对其进行排序
最佳答案
您可以使用此:
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]);
});