Working on an application where the database response lacks a direct timestamp, causing data to render differently each day and requiring me to use .reverse() to fix my charts. I aim to implement a permanent solution using sort(), but struggling due to the absence of direct timestamps in the response.
The API provides examples responses as follows:
"records": [
{
"dateRep": "01/05/2020",
"day": "1",
"month": "5",
"year": "2020",
"cases": "222",
"deaths": "4",
"countriesAndTerritories": "Afghanistan",
"geoId": "AF",
"countryterritoryCode": "AFG",
"popData2018": "37172386",
"continentExp": "Asia"
},
{
"dateRep": "30/04/2020",
"day": "30",
"month": "4",
"year": "2020",
"cases": "122",
"deaths": "0",
"countriesAndTerritories": "Afghanistan",
"geoId": "AF",
"countryterritoryCode": "AFG",
"popData2018": "37172386",
"continentExp": "Asia"
},
{
"dateRep": "29/04/2020",
"day": "29",
"month": "4",
"year": "2020",
"cases": "124",
"deaths": "3",
"countriesAndTerritories": "Afghanistan",
"geoId": "AF",
"countryterritoryCode": "AFG",
"popData2018": "37172386",
"continentExp": "Asia"
},
The current code is not functioning as expected:
for (var i = 0; i < countriesList.length; i++) {
dataWorldWideEuropa.sort(
(a, b) => new Date(a.year,a.month,a.day) > new Date(b.year,b.month,b.day)
).map(datum => {
// do work here
});
}
Is there a way to convert the .day, .month, and .year fields from the response into a Date format for sorting purposes without relying on .reverse() when the API changes?