I have some .json files to read with NodeJs. These files contain a date, a title and the content.
{
"date": "10.06.2018",
"title": "title goes here",
"content": "content goes here"
}
As you can see the date got the wrong format, it's the german date format. When reading the directory I want to sort the files by their date property.
Currently I just read the files and try to compare the date property
const path = 'articles'; // the path to start from
const directoryItems = fs.readdirSync(path); // get all the files from the directory
const articles = directoryItems.map(file => JSON.parse(fs.readFileSync(`${path}/${file}`))); // convert the files to objects
const sortedArticles = articles.sort((currentFile, otherFile) => currentFile.date < otherFile.date); // sort the objects by their date
Do I have to convert the date to a valid JavaScript date format first?