Tuesday, 4 January 2011

Array Sort - Number expected in Internet Explorer

While working on sorting array for date with following code
/*sort the array with date*/
data.sort(function(a, b) {
var objA = a.date;
var objB = b.date;
var dateA = new Date(objA);
var dateB = new Date(objB);
return dateA - dateA;
});
i faced "Number expected" error in IE while it was working fine in other browsers.

Fix - Parse the return as Int like this
/*sort the array with date*/
data.sort(function(a, b) {
var objA = a.date;
var objB = b.date;
var dateA = new Date(objA);
var dateB = new Date(objB);
return parseInt(dateA - dateA);
});

No comments:

Post a Comment