For some reason I can't sort my Array of Objects using Sort Function:
FullHistory.sort(function(a, b){
return a.Date.localeCompare(b.Date);
});
When I use console.log() - It returns: -1/0/1, but the Array's Order is not changing at all
The rest of my Code:
let FullHistory = [];
CurrentTitle = $(data).find(".normal_header").text().replace(" Episode Details", "");
$($(data).find('.spaceit_pad').get().reverse()).each(function () {
FullHistory.push({ Title: CurrentTitle, Ep: $(this).text().replace("Ep ", "").split(',')[0], Date: $(this).append(":00").text().replace("Ep ", "").replace("watched", "").replace(" Remove", "").split(",")[1].replace("on ", "").replace("at ", "").replace(" ", "") });
});
I tried: changing let to var, using if statements in Sort Function, changing types, using Lodash Lib.
Your Date properties will be sorted as strings -- that is what localeCompare does. It doesn't look at those strings as representing dates. Since the date format used is not ISO, the lexical order of those strings does match the order you would expect for the dates.
So convert the strings to numerical epochs:
let FullHistory = [
{Title: "Grand Blue", Ep: "1", Date: "02/19/2020 23:36:00" },
{Title: "Darling in the FranXX", Ep: "1", Date: "03/04/2019 23:44:00" },
];
FullHistory.sort(function(a, b){
return Date.parse(a.Date) - Date.parse(b.Date);
});
console.log(FullHistory);