I have a function which orders dates based on a pivot. Note that this function ignores the year of the supplied dates. For example, given a pivot date of 1-July, all dates will be ordered from 1-July to 31-June. The problem seems to be with my sort compare function which works perfectly fine in Chrome and Firefox, however fails in IE. The resulting statsDataPoints (Array) does not get sorted.
This is what the dates array looks like:
[{"Min":"6.0", "Mean":"10.8", "Max":"32.1", "StdDev":"7.5", "LowerPercentile":"6.7", "Median":"8.0", "UpperPercentile":"11.2", "_Name":"30-Mar"}, {"Min":"6.0", "Mean":"11.1", "Max":"31.7", "StdDev":"7.4", "LowerPercentile":"7.3", "Median":"8.7", "UpperPercentile":"11.1", "_Name":"31-Mar"}, {"Min":"6.0", "Mean":"10.9", "Max":"31.4", "StdDev":"7.3", "LowerPercentile":"7.2", "Median":"8.4", "UpperPercentile":"11.0", "_Name":" 1-Apr"}, ... ]
This is my function:
/**
* Returns a function which orders dates based on a pivot. Note that this function ignores the year of the
* supplied dates. It is easiest to illustrate how this function works with an example:
* Given a pivot date of 1-July, all dates will be ordered from 1-July to 31-June.
*
* @param {Array} dates An array of objects containing a date key.
* @param {String} pivot The pivot date we are sorting from (should be in the same format as dateKey)
*/
var dummyYear = 2000;
var pivot = moment(readingStartDate).format("DD-MMM");
pivot = moment(pivot, "DD-MMM").year(dummyYear);
statsDataPoints.sort(function(aDate, bDate) {
var a = moment(aDate["_Name"], "DD-MMM").year(dummyYear).toDate();
var b = moment(bDate["_Name"], "DD-MMM").year(dummyYear).toDate();
if (((a < pivot) && (pivot < b)) || (a > b)) {
return 1;
} else if (((b < pivot) && (pivot < a)) || (a < b)) {
return -1;
}
return 0;
});