I've been analyzing this JavaScript code for quite some time now and I'm stuck.
It's a case-insensitive sort function that handles special (Danish) characters properly, but for some reason, it doesn't work in IE7. I can't figure out why because it doesn't seem too complex...
function _compare(a, b) {
var sortorder = " 0123456789.abcdefghijklmnopqrstuvwxyzæøå",
min = 0,
idx = 0,
c = 0;
a = ' ' + a;
b = ' ' + b;
a = a.substring(1).toLowerCase();
b = b.substring(1).toLowerCase();
min = Math.min(a.length, b.length);
while (idx < min && a[idx] == b[idx]) {
idx++;
}
if (idx == min) {
if (a.length > b.length) {
c = 1;
}
else if (a.length < b.length) {
c = -1;
}
}
else {
c = (sortorder.indexOf(a[idx]) > sortorder.indexOf(b[idx])) ? 1 : -1;
}
return c;
}
var key, ar = [];
ar.push("TEST");
ar.push("abcd");
ar.push("test");
ar.push("øæå!");
ar.push("oåø!");
ar.push("åæø!");
ar.push("aaø!");
ar.sort(_compare);
for (key in ar) {
$("pre").append(ar[key] + "<br />");
}
http://jsfiddle.net/hpvek/ - it works as expected in FF, Chrome, IE8-9 and Safari. IE7 (possibly IE6, which I don't have access to for testing) seems to be the outlier here.
Thank you for any assistance.