fix: sorting by date

This commit is contained in:
Barrett Ruth 2025-10-12 11:31:00 -04:00
parent 38f2677cd2
commit 5339527461
5 changed files with 20 additions and 35 deletions

View file

@ -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);
---
<BaseLayout title={title}>

View file

@ -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);
---
<BaseLayout title={title}>

View file

@ -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);
---
<BaseLayout title={title}>

View file

@ -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,

12
src/utils/sort.js Normal file
View file

@ -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;
};