0

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'];

I use this for every other browser except IE8:

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]);
});

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

6
  • Have you tried debugging it IE developer tools? Commented May 30, 2013 at 8:17
  • 2
    Have a look here - stackoverflow.com/questions/1744310/…. Have you implemented a workaround for the indexOf functionality? array.indexOf() is not supported in IE8 Commented May 30, 2013 at 8:18
  • In particular, you need to use what is called a "polyfill" to patch in the missing functionality. Here is one you can copy/paste: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 30, 2013 at 8:22
  • I looked at tha, but I have no idea how to implement that into my code. Every time I try to implement it, all browsers stop working. Commented May 30, 2013 at 8:23
  • @user1350637: Then you are making some type of elementary syntax error. Look at the console of a developer-friendly browser for error messages. Commented May 30, 2013 at 8:23

1 Answer 1

2

IE8 does not support array.indexOf(). You'll need to find an alternative.

Possible solutions:

  1. Use jQuery (or a similar library), which gives $.inArray() as a direct replacement for array.indexOf().

  2. Use a polyfill library that adds a replacement .indexOf method to the Array prototype. Here's one possible option. (others available via google of course)

  3. Rewrite your code to search through the array using a simple loop.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.