IE9 javascript sort order … why?


I noticed IE9 sort order is changing elements order when comparison function returns 0.


var myarray=[ {id:1,val:0}, {id:2,val:0}, {id:3,val:7}, {id:4,val:41} ]; myarray.sort(function(a,b){return a.val - b.val}); for(var i in myarray) { console.log(myarray[i].id); }

Current stable versions of Chrome, Firefox, Opera and Safari got the following output: 1 2 3 4.

Same output for IE7 and IE8.

IE9 output is: 2 1 3 4

Why? Is that normal?


From MDC (emphasis mine):

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. [Note: the ECMAscript standard does not guarantee this behaviour], and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

In my experience, only Chrome/Firefox get this right. Opera 11's behavior for me is .. not well defined.

E.g., using sort to move all zeroes to the top of the array:

[1, 0, 3, 0, 5, 0, 2].sort(function (a, b) { return b === 0 && 1 || 0;});
  • Chromium 10: [0, 0, 0, 1, 3, 5, 2]
  • Firefox 4: [0, 0, 0, 1, 3, 5, 2]
  • Opera 11: [0, 0, 0, 2, 1, 5, 3] <- does not maintain order of non-zeroes


Don't use for...in on an array if you're trying to iterate over the numeric properties, for two reasons:

  • You will also get methods and properties added to Array.prototype showing up;
  • The iteration order is defined in the ECMAScript spec as being implementation-dependent, meaning it could in theory be anything.

Both points also apply to Objects. Chrome in fact does not conform to the most common browser behaviour, leading to heated debate in a Chrome bug report.


Based on your sort function, both of those elements are equal and it shouldn't matter which order they appear in. It is up to the browser to either leave the order as it is or switch the order as it sees appropriate...neither is a guarantee.

If the two aren't equal, then your sort function is incorrect and should take the other items into account as well.


  • Java: can you change the value of the same string every time it loops?
  • How do I choose between innerText or nodeValue?
  • Java - volatile variable is not updating
  • How to Implement DialogBox and onLongClick at the same time on the button?
  • Adding context path to SLF4J log statements
  • Warning - Initialization makes pointer from integer without a cast
  • Log exceptions in separate files for each tenant in multi-tenancy architecture
  • CountDownTimer trivia game score - Android (java)
  • @font-face in CSS…is it ok?
  • How to enable autocomplete when using Internet Explorer and SSL?
  • Flex item is not shrinking smaller than its content [duplicate]
  • how to change url in browser url box?
  • Java: Issue with available() method of BufferedInputStream
  • Is static member variable initialized in a template class if the static menber is not used?
  • Capture video without preview
  • What part of this RegEx is causing it to find no matches in IE7 and IE8?
  • 'data' is null or not an object IE8
  • position: fixed with margin: auto in IE9/10
  • Prevent text selection when dragging from outside target in IE7/8
  • white-space: pre-wrap not working in IE
  • Why does Internet Explorer sometimes trigger a local storage event before the data's ready? [du
  • How can I get data in json array by ID
  • Javascript - Waiting for event before proceeding
  • Grails eager fetch doesn't retrieve all data
  • C# Remove URL from String
  • Rails AREL .where statement
  • Cursor in wrong place in contenteditable
  • Entity Framework ObjectContext: Concurrency
  • Is it safe to drop the -webkit vendor prefix from the css3 border-radius yet?
  • Want to understand iframe breakout code
  • PayPal API Listener Website Payments Standard URI
  • AppleScript : find open tab in safari by name and open it
  • What is the purpose of TaskExecutor in spring?
  • MySQL Order by column = x, column asc?
  • Recording logins for password protected directories
  • Pass value from viewmodel to script in zk
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Benchmarking RAM performance - UWP and C#
  • python draw pie shapes with colour filled
  • How to Embed XSL into XML