In Next.js site (here https://nextjs.org/learn/basics/data-fetching/implement-getstaticprops), this example :
import path from 'path'
import matter from 'gray-matter'
const postsDirectory = path.join(process.cwd(), 'posts')
export function getSortedPostsData() {
// Get file names under /posts
const fileNames = fs.readdirSync(postsDirectory)
const allPostsData = fileNames.map(fileName => {
// Remove ".md" from file name to get id
const id = fileName.replace(/\.md$/, '')
// Read markdown file as string
const fullPath = path.join(postsDirectory, fileName)
const fileContents = fs.readFileSync(fullPath, 'utf8')
// Use gray-matter to parse the post metadata section
const matterResult = matter(fileContents)
// Combine the data with the id
return {
id,
...matterResult.data
}
})
// Sort posts by date
return allPostsData.sort(({ date: a }, { date: b }) => {
if (a < b) {
return 1
} else if (a > b) {
return -1
} else {
return 0
}
})
}
In this example the return value of function getSortedPostsData() is a sorted list of allPostsData. Posts are sorted on the basis of date.
My question is how the array is being sorted ?
The sort function is taking two arguments {date:a} , {date:b}. Is it automatically converting the arguments to Date Object as the a.date and b.date are strings