From 5339527461550ef73b3c1c21354a97cb66be9567 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 12 Oct 2025 11:31:00 -0400 Subject: [PATCH] fix: sorting by date --- src/pages/[category]/index.astro | 10 ++-------- src/pages/gist.astro | 10 ++-------- src/pages/git.astro | 10 ++-------- src/pages/index.astro | 13 ++----------- src/utils/sort.js | 12 ++++++++++++ 5 files changed, 20 insertions(+), 35 deletions(-) create mode 100644 src/utils/sort.js diff --git a/src/pages/[category]/index.astro b/src/pages/[category]/index.astro index 8bae471..c105524 100644 --- a/src/pages/[category]/index.astro +++ b/src/pages/[category]/index.astro @@ -1,6 +1,7 @@ --- import BaseLayout from "../../layouts/BaseLayout.astro"; import { getCollection } from "astro:content"; +import { sortItem } from "../../utils/sort.js"; import * as collections from "../../content/config"; export async function getStaticPaths() { @@ -15,14 +16,7 @@ const category = Astro.params.category; const title = "Barrett Ruth"; const posts = await getCollection(category); -posts.sort((a, b) => { - const parseEuroDate = (dateStr) => { - if (!dateStr) return new Date(0); - const [day, month, year] = (dateStr || "").split("/"); - return new Date(year, month - 1, day); - }; - return parseEuroDate(b.data.date) - parseEuroDate(a.data.date); -}); +posts.sort(sortItem); --- diff --git a/src/pages/gist.astro b/src/pages/gist.astro index b74537e..fb6be39 100644 --- a/src/pages/gist.astro +++ b/src/pages/gist.astro @@ -1,17 +1,11 @@ --- import BaseLayout from "../layouts/BaseLayout.astro"; import { getCollection } from "astro:content"; +import { sortItem } from "../utils/sort.js"; const title = "gists"; const gists = await getCollection("gists"); -gists.sort((a, b) => { - const parseEuroDate = (dateStr) => { - if (!dateStr) return new Date(0); - const [day, month, year] = (dateStr || "").split("/"); - return new Date(year, month - 1, day); - }; - return parseEuroDate(b.data.date) - parseEuroDate(a.data.date); -}); +gists.sort(sortItem); --- diff --git a/src/pages/git.astro b/src/pages/git.astro index 6c279fa..857afcf 100644 --- a/src/pages/git.astro +++ b/src/pages/git.astro @@ -1,17 +1,11 @@ --- import BaseLayout from "../layouts/BaseLayout.astro"; import { getCollection } from "astro:content"; +import { sortItem } from "../utils/sort.js"; const title = "git repos"; const repos = await getCollection("git"); -repos.sort((a, b) => { - const parseEuroDate = (dateStr) => { - if (!dateStr) return new Date(0); - const [day, month, year] = (dateStr || "").split("/"); - return new Date(year, month - 1, day); - }; - return parseEuroDate(b.data.date) - parseEuroDate(a.data.date); -}); +repos.sort(sortItem); --- diff --git a/src/pages/index.astro b/src/pages/index.astro index d96478f..7cc24ea 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,24 +1,15 @@ --- import BaseLayout from "../layouts/BaseLayout.astro"; +import { sortItem } from "../utils/sort.js"; import { getCollection } from "astro:content"; const title = "Barrett Ruth"; const CATS = ["algorithms", "software", "meditations", "autonomous-racing"]; -function parseEuroDate(dateStr) { - if (!dateStr) return new Date(0); - const [d, m, y] = (dateStr || "").split("/"); - return new Date(Number(y), Number(m) - 1, Number(d)); -} - const postsByCategory = {}; for (const c of CATS) { const entries = await getCollection(c); - entries.sort( - (a, b) => - parseEuroDate(b.data.date).getTime() - - parseEuroDate(a.data.date).getTime(), - ); + entries.sort(sortItem); postsByCategory[c] = entries.map((e) => ({ id: `${c}/${e.slug}.mdx`, slug: e.slug, diff --git a/src/utils/sort.js b/src/utils/sort.js new file mode 100644 index 0000000..cea5e54 --- /dev/null +++ b/src/utils/sort.js @@ -0,0 +1,12 @@ +const parseEuroDate = (dateStr) => { + if (!dateStr) return new Date(0); + const [d, m, y] = (dateStr || "").split("/"); + return new Date(Number(y), Number(m) - 1, Number(d)); +}; + +export const sortItem = (a, b) => { + const aDate = parseEuroDate(a.data.date).getTime(); + const bDate = parseEuroDate(b.data.date).getTime(); + + return aDate === bDate ? a.slug.localeCompare(b.slug) : bDate - aDate; +};