Js Sort Works In Firefox But Not Ie - Can't Work Out Why
I have a line in a javascript function that sort an array of objects based on the order of another array of strings. This is working in firefox but not in IE and i don't know why.
Solution 1:
First, there's no indexOf
method of Array in IE <= 8. You'll need to write your own. Second, the comparison function passed to the sort()
method of an Array should return a number rather than a Boolean.
var indexOf = (typeof Array.prototype.indexOf == "function") ?
function(arr, val) {
return arr.indexOf(val);
} :
function(arr, val) {
for (var i = 0, len = arr.length; i < len; ++i) {
if (typeof arr[i] != "undefined" && arr[i] === val) {
return i;
}
}
return -1;
};
csubSorted = csub.sort(function(a,b){
return indexOf(originalData, a.value) - indexOf(originalData, b.value);
});
Post a Comment for "Js Sort Works In Firefox But Not Ie - Can't Work Out Why"