The dates look like this:
[‘Oct 7, 2009’, ‘Nov 10, 2009’, ‘Jan 10, 2009’, ‘Oct 22, 2009’, …]
The month is always the first three characters of full month name (‘January’ => ‘Jan’, ‘February’ => ‘Feb’, …).
The day is one or two digits (1, 2, … 31), with no preceding zero.
There is always a comma after the day. The year is always four digits.
I'm trying to write a function that will order this list of string in date by descending order
This is what I have currently:
let dates = [
'Oct 7, 2009',
'Nov 10, 2009',
'Jan 10, 2009',
'Oct 22, 2009'
]
let sortDate = function (date1, date2) {
if (date1 > date2) return -1
if (date1 < date2) return 1
return 0
}
dates.sort(sortDate)
for (let i = 0; i < dates.length; i++) {
document.write(i + ': ' + dates[i])
}
The output shows this though:
0: Oct 7, 2009
1: Oct 22, 2009
2: Nov 10, 2009
3: Jan 10, 2009
January should be first.