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 BaseLayout from "../../layouts/BaseLayout.astro";
import { getCollection } from "astro:content"; import { getCollection } from "astro:content";
import { sortItem } from "../../utils/sort.js";
import * as collections from "../../content/config"; import * as collections from "../../content/config";
export async function getStaticPaths() { export async function getStaticPaths() {
@ -15,14 +16,7 @@ const category = Astro.params.category;
const title = "Barrett Ruth"; const title = "Barrett Ruth";
const posts = await getCollection(category); const posts = await getCollection(category);
posts.sort((a, b) => { posts.sort(sortItem);
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);
});
--- ---
<BaseLayout title={title}> <BaseLayout title={title}>

View file

@ -1,17 +1,11 @@
--- ---
import BaseLayout from "../layouts/BaseLayout.astro"; import BaseLayout from "../layouts/BaseLayout.astro";
import { getCollection } from "astro:content"; import { getCollection } from "astro:content";
import { sortItem } from "../utils/sort.js";
const title = "gists"; const title = "gists";
const gists = await getCollection("gists"); const gists = await getCollection("gists");
gists.sort((a, b) => { gists.sort(sortItem);
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);
});
--- ---
<BaseLayout title={title}> <BaseLayout title={title}>

View file

@ -1,17 +1,11 @@
--- ---
import BaseLayout from "../layouts/BaseLayout.astro"; import BaseLayout from "../layouts/BaseLayout.astro";
import { getCollection } from "astro:content"; import { getCollection } from "astro:content";
import { sortItem } from "../utils/sort.js";
const title = "git repos"; const title = "git repos";
const repos = await getCollection("git"); const repos = await getCollection("git");
repos.sort((a, b) => { repos.sort(sortItem);
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);
});
--- ---
<BaseLayout title={title}> <BaseLayout title={title}>

View file

@ -1,24 +1,15 @@
--- ---
import BaseLayout from "../layouts/BaseLayout.astro"; import BaseLayout from "../layouts/BaseLayout.astro";
import { sortItem } from "../utils/sort.js";
import { getCollection } from "astro:content"; import { getCollection } from "astro:content";
const title = "Barrett Ruth"; const title = "Barrett Ruth";
const CATS = ["algorithms", "software", "meditations", "autonomous-racing"]; 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 = {}; const postsByCategory = {};
for (const c of CATS) { for (const c of CATS) {
const entries = await getCollection(c); const entries = await getCollection(c);
entries.sort( entries.sort(sortItem);
(a, b) =>
parseEuroDate(b.data.date).getTime() -
parseEuroDate(a.data.date).getTime(),
);
postsByCategory[c] = entries.map((e) => ({ postsByCategory[c] = entries.map((e) => ({
id: `${c}/${e.slug}.mdx`, id: `${c}/${e.slug}.mdx`,
slug: e.slug, 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;
};