I am having an odd problem with an array of dates not being fully sorted. The majority of the array seems to be sorting correctly, however there is an oddity where the first two elements in the array are not being sorted, or are being sorted incorrectly.
Code:
var arrSortTest = ["July 11, 1960", "February 1, 1974", "July 11, 1615", "October 18, 1851", "November 12, 1995"];
for (var i = 0; i < arrSortTest.length; i++) {
arrSortTest.sort(function(i) {
var temp = new Date(i);
return temp
});
}
console.log(arrSortTest)
What I expected: ["July 11, 1615", "October 18, 1851", "July 11, 1960", "February 1, 1974", "November 12, 1995"]
What I get: ["October 18, 1851", "July 11, 1615", "July 11, 1960", "February 1, 1974", "November 12, 1995"]
The above code seems like it should just work, and it seems like it does for the most part. What should I be doing differently here to get the dates in my test array sorted from oldest to newest? Am I just not understanding how sorting an array with dates even works?
Thank you in advance.