How to sort next.js blog posts by date

Convert the date string to a date object

npm install Luxon
/components/postList/postList.jsimport { dateTime } from 'Luxon'
DateTime.fromFormat('09-06-2022, 'm-d-yyyy')

Order blog post by most recent date

sortBlogPostsByDate = posts.sort((a, b) => {
const beforeDate = DateTime.fromFormat(, 'm-d-yyyy')const afterDate = DateTime.fromFormat(, 'm-d-yyyy')return afterDate - beforeDate})
return (<div className="postlist">{posts && => { return (   <DivList key={post.slug}>    <Link href={{ pathname: `/${post.slug}` }}>     <PostTitle href={`/${post.slug}`}>       {post.frontmatter.title}     </PostTitle>    </Link>    <PostData>

<PostDate className="post-date"> {} </PostDate> </PostData> <PostText>{post.frontmatter.description}</PostText> </DivList>

})} </div>)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store