1

I have got an array like this:

[1, Stopped]
[2, Waiting]
[3, Finished]
[4, Stopped]
[5, Running]

The number is the id of the program and the text is the status of the program. I need to sort this array according to the next order:

['Error','Halted','Blocked','Finished','Waiting to Start','Waiting','Stopping','Running','Idle','Stopped','Opened','Ready'];

Can anyone tell me how I can sort the array using the predefined order?

It is working in all different browsers except for IE8. Can anyone tell me how to sort it in IE8

2
  • Not much, I have looked al over this website and google for more information. But all I can find is sorting alphabetically and so on. Commented Apr 15, 2013 at 9:30
  • It is working in all different browsers except for IE8. Can anyone tell me how to sort it in IE8 Commented May 30, 2013 at 7:59

1 Answer 1

2

You can use this:

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]);
});
2
  • 1
    It is working in all different browsers except for IE8. Can anyone tell me how to sort it in IE8 Commented May 30, 2013 at 7:57
  • It doesn't work because of indexOf. I've edited my answer with solution working in IE8 too. Commented May 31, 2013 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.