Skip to content

Instantly share code, notes, and snippets.

@zackd
Created January 31, 2011 13:22
Show Gist options
  • Save zackd/804017 to your computer and use it in GitHub Desktop.
Save zackd/804017 to your computer and use it in GitHub Desktop.
test proves IE array.sort unreliable
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
</head>
<body>
<!-- source: http://www.zachleat.com/web/2010/02/24/array-sort/ -->
<!-- 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;
});
*/
-->
<pre><script type="text/javascript">
function test(num)
{
try {
var obj = [];
for(var j=0, k=num; j<k; j++) {
obj.push('ABCD'+j);
}
obj.sort(function(m,p){
/*
works:
m.replace('nomatch', ''),
m.concat(''),
''+m
fails:
new String(old),
m.substr(0),
m.substring(0),
m.split('nomatch')[0],
m.slice(0),
m.toLowerCase(),
m.toUpperCase(),
m+'newstring',
m.concat('newstring')
(''+m).toLowerCase()
(''+m).toUpperCase()
m.concat('').toLowerCase();
m.concat('').toUpperCase();
m='new string';
*/
m=(''+m).toLowerCase();
p=(''+p).toLowerCase();
//document.writeln(' |' + m + '|' + p + '| ');
//document.writeln(' |' + typeof m + '|' + typeof p + '| ');
if(m > p){
return 1;
}
if(m < p) {
return -1;
}
return 0;
});
document.writeln('<span style="color: green">Success (' + obj.length + ')</span> ');
} catch(e) {
document.writeln('<span style="color: red">Failed (' + obj.length + ') ' + e.message + '</span>');
}
}
for(var j=1, k=150; j<=k; j++) {
test(j);
}
</script></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment