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?

Spudley
  • 166,037
  • 39
  • 233
  • 307
Extigo
  • 79
  • 2
  • 8
  • Have you tried debugging it IE developer tools? – Simon May 30 '13 at 08:17
  • 2
    Have a look here - http://stackoverflow.com/questions/1744310/how-to-fix-array-indexof-in-javascript-for-ie-browsers. Have you implemented a workaround for the indexOf functionality? `array.indexOf()` is not supported in IE8 – Mark Walters May 30 '13 at 08: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: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Compatibility – Jon May 30 '13 at 08: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. – Extigo May 30 '13 at 08:23
  • @user1350637: Then you are making some type of elementary syntax error. Look at the console of a developer-friendly browser for error messages. – Jon May 30 '13 at 08:23
  • Ok, just to be certain. I need to paste the code mentioned in the mozilla page at the beginning of my .js file? Or just in the beginning of the function I used it in? – Extigo May 30 '13 at 08:28

1 Answers1

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307