From a0d850c130fbbd4f94ad74a2eab96605c0c10eb9 Mon Sep 17 00:00:00 2001 From: Sat Naing Date: Sat, 10 Sep 2022 16:22:30 +0630 Subject: [PATCH] Create getSortedPosts util function --- src/utils/getSortedPosts.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/utils/getSortedPosts.ts diff --git a/src/utils/getSortedPosts.ts b/src/utils/getSortedPosts.ts new file mode 100644 index 0000000..d032202 --- /dev/null +++ b/src/utils/getSortedPosts.ts @@ -0,0 +1,20 @@ +import type { MarkdownInstance } from "astro"; + +interface Frontmatter { + title: string; + description: string; + author: string; + datetime: string; + slug: string; + featured: boolean; + tags: string[]; +} + +const getSortedPosts = (posts: MarkdownInstance[]) => + posts.sort( + (a, b) => + Math.floor(new Date(b.frontmatter.datetime).getTime() / 1000) - + Math.floor(new Date(a.frontmatter.datetime).getTime() / 1000) + ); + +export default getSortedPosts;