Internet Explorer Array.sort Unreliable

What would you expect to be the result of executing the following code?
// Create a medium size array, at least 100 items
var obj = [];
for(var j=0, k=150; j<k; j++) {
// the value here doesn’t matter.
obj.push(‘ABCD’+j);
}

// Sort the…


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman

What would you expect to be the result of executing the following code?

// Create a medium size array, at least 100 items
var obj = [];
for(var j=0, k=150; j<k; j++) {
    // the value here doesn't matter.
    obj.push('ABCD'+j);
}
 
// Sort the array alphabetically.
obj.sort(function(m,p){
    m=(''+m).toLowerCase();
    p=(''+p).toLowerCase();
 
    if(m > p) return 1;
    if(m < p) return -1;
    return 0;
});

The obj Array should now be sorted, in alphabetical order based on value. BUT, in our best friend Internet Explorer, a Number Expected error may be the result. Don’t be fooled if your test array behaves correctly, it only happens intermittently for arrays of varying size!

So, I whipped up a quick test to check the damage. Iterating over array sizes from 1 to 150, running the Array sort algorithm. The following failures resulted:

Browser Failures *
Internet Explorer 6 4 sizes out of 150
Internet Explorer 7 18 sizes out of 150
Internet Explorer 8 (and Compatibility Mode) 2 sizes out of 150
Internet Explorer 9 (and Compatibility Mode) 0 sizes out of 150 (Fixed!)

* Failures may vary to the specifications of the test machine.

Of course, the Number Expected error is going to result whenever your code doesn’t return a number inside of the function callback. But the problem here is something deeper than simple application code failure. The problem is in JScript itself. Any modification to the sort arguments may result in the Number Expected error.

// modifies the argument m and is unreliable.
m=(''+m).toLowerCase();

This problem will manifest itself more frequently if you use the Google Closure Compiler, which restructures JavaScript to reuse argument variables if possible, probably to save the 4 character penalty of a “var ” declaration.

Normally, reusing argument variables is a safe practice for primitives, since they are passed by value and not by reference, as is the case in this Array sort example 1. So, what exactly is going on here? One commenter at the Google Closure Compiler bug seems to think that the actual array values are being passed by reference instead of by value. I’m not completely convinced that’s the case.

The Fix #

Don’t reuse the argument variables inside of an Array sort function.

// Changing the above example
obj.sort(function(m1,p1){
    var m=(''+m1).toLowerCase(),
        p=(''+p1).toLowerCase();
 
    if(m > p) return 1;
    if(m < p) return -1;
    return 0;
});

Check the source code of the demo file to see the different methods of modifying the arguments that I attempted.

  1. If you want to learn more about passing by value or reference, Jonathan Snook has a nice explanation.

Internet Explorer can’t make you do anything, it can only make you wish you hadn’t.

Updated: Added note about Internet Explorer 9 and its Compatibility View. Looks like it’s fixed!


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman


Print Share Comment Cite Upload Translate Updates
APA

Zach Leatherman | Sciencx (2010-02-24T06:00:00+00:00) Internet Explorer Array.sort Unreliable. Retrieved from https://www.scien.cx/2010/02/24/internet-explorer-array-sort-unreliable/

MLA
" » Internet Explorer Array.sort Unreliable." Zach Leatherman | Sciencx - Wednesday February 24, 2010, https://www.scien.cx/2010/02/24/internet-explorer-array-sort-unreliable/
HARVARD
Zach Leatherman | Sciencx Wednesday February 24, 2010 » Internet Explorer Array.sort Unreliable., viewed ,<https://www.scien.cx/2010/02/24/internet-explorer-array-sort-unreliable/>
VANCOUVER
Zach Leatherman | Sciencx - » Internet Explorer Array.sort Unreliable. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2010/02/24/internet-explorer-array-sort-unreliable/
CHICAGO
" » Internet Explorer Array.sort Unreliable." Zach Leatherman | Sciencx - Accessed . https://www.scien.cx/2010/02/24/internet-explorer-array-sort-unreliable/
IEEE
" » Internet Explorer Array.sort Unreliable." Zach Leatherman | Sciencx [Online]. Available: https://www.scien.cx/2010/02/24/internet-explorer-array-sort-unreliable/. [Accessed: ]
rf:citation
» Internet Explorer Array.sort Unreliable | Zach Leatherman | Sciencx | https://www.scien.cx/2010/02/24/internet-explorer-array-sort-unreliable/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.