To convert the strings into Date() objects, you need to handle the date string parsing differently due to IE's inadequate implementation. However, you can achieve consistency across browsers by using setDate() and setMonth() functions, which both accept numerical values - 1-31 for setDate() and 0-11 for setMonth(). Creating a map of month names can simplify this process.
Here is an approach that typically works:
(function () {
// Declare two date objects and a month names/numbers map
var firstDate = new Date();
var secondDate = new Date();
var monthMap = {
Jan: 0,
Feb: 1,
Mar: 2,
Apr: 3,
May: 4,
Jun: 5,
Jul: 6,
Aug: 7,
Sep: 8,
Oct: 9,
Nov: 10,
Dec: 11
};
MyArrayOfDates.sort(function (date1, date2) {
// Split the date strings into [ day, month ]
var firstDateString = date1.split(' '),
secondDateString = date2.split(' ');
// Update the Date() objects with the dates from the strings
firstDate.setDate(firstDateString[0]);
firstDate.setMonth(monthMap[firstDateString[1]]);
secondDate.setDate(secondDateString[0]);
secondDate.setMonth(monthMap[secondDateString[1]]);
// Subtracting date2 from date1 converts the Date object to a number
return firstDate - secondDate;
});
})();
//-> ["09 Jun", "13 Jun", "30 Jun", "13 Aug", "25 Aug"]
You can view a demonstration here - http://jsfiddle.net/Tw6xt/