To sort posts in Astro, get the collection and sort the array yourself: posts.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()) puts the newest first. getCollection() does not promise any order, so always sort explicitly, and make sure pubDate is a real Date by using z.coerce.date() in the schema.
Key Takeaways
getCollection()order is not guaranteed; never rely on file names or load order.- Use
z.coerce.date()sopubDateis aDate, not a string.- Subtract
valueOf()values for a numeric, reliable comparison.- Add a tiebreaker so posts on the same day keep a stable order.
- Put sorting in one helper that every listing imports.
Make sure dates are dates
Sorting strings like "2026-11-02" happens to work for ISO dates but breaks the moment someone writes "Nov 2, 2026". Coerce in the schema:
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const blog = defineCollection({
loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
schema: z.object({
title: z.string(),
pubDate: z.coerce.date(),
updatedDate: z.coerce.date().optional(),
}),
});
export const collections = { blog };
Invalid dates now fail the build with the file name, instead of quietly sorting to the wrong place.
Sort newest first
// src/lib/posts.ts
import { getCollection, type CollectionEntry } from 'astro:content';
type Post = CollectionEntry<'blog'>;
export const byNewest = (a: Post, b: Post) =>
b.data.pubDate.valueOf() - a.data.pubDate.valueOf() || a.id.localeCompare(b.id);
export async function getPosts() {
const posts = await getCollection('blog');
return posts.sort(byNewest);
}
valueOf() returns milliseconds since 1970, so subtraction gives the negative, zero or positive number sort() expects. The || a.id.localeCompare(b.id) part is the tiebreaker: two posts published on the same day sort alphabetically by ID, so the order never shifts between builds. For oldest first, swap a and b in the subtraction.
Then use it in the index:
---
// src/pages/blog/index.astro
import { getPosts } from '../../lib/posts';
const posts = await getPosts();
---
<ul>
{posts.map((post) => (
<li>
<a href={`/blog/${post.id}/`}>{post.data.title}</a>
<time datetime={post.data.pubDate.toISOString()}>
{post.data.pubDate.toLocaleDateString('en-US', { dateStyle: 'medium' })}
</time>
</li>
))}
</ul>
Sort by last updated
For a “recently updated” list, fall back to pubDate when a post has never been updated:
// src/lib/posts.ts
const lastChanged = (post: Post) => (post.data.updatedDate ?? post.data.pubDate).valueOf();
export const byRecentlyUpdated = (a: Post, b: Post) => lastChanged(b) - lastChanged(a);
Keep the main blog index sorted by pubDate, though. Readers expect a chronological archive, and resorting old posts to the top every time you fix a typo is confusing.
Sort once, slice later
sort() changes the array in place. That is fine for an array you just fetched, but if you share one sorted array between components, copy it before sorting differently:
const latest = (await getPosts()).slice(0, 3);
const alphabetical = [...(await getPosts())].sort((a, b) =>
a.data.title.localeCompare(b.data.title),
);
getCollection() returns a fresh array on each call, but copying makes the intent clear. With the sorted list in hand, a homepage “latest posts” block is slice(0, 3), and pagination takes the same array.
Sorting is also the base for previous and next post links and year-by-year archives, which both depend on a stable order. The content collections docs explain getCollection() filters, and the Astro API reference has the signature. Blog themes in our Blog & Editorial category ship with this kind of helper already in place.
FAQ
What order does Astro getCollection return?
No guaranteed order. It often looks alphabetical by file, but that is not part of the API and can differ between loaders and platforms. Always sort the result.
How do I sort Astro posts oldest first?
Reverse the subtraction: a.data.pubDate.valueOf() - b.data.pubDate.valueOf(). Everything else stays the same.
Why does sorting my Astro posts give the wrong order?
Usually pubDate is a string, not a Date, because the schema uses z.string(). Switch to z.coerce.date() and compare with valueOf().
How do I sort Astro posts by title?
Use localeCompare: posts.sort((a, b) => a.data.title.localeCompare(b.data.title)). It handles accents and case better than comparing strings with less-than.