Skip to content

Commit 1b90683

Browse files
committed
Sort collections explicitly (lexicographically by id)
Per https://docs.astro.build/en/guides/upgrade-to/v5/#breaking-changes-to-legacy-content-and-data-collections > Sort order of generated collections is non-deterministic and platform-dependent. This means that if you are calling getCollection(), the order in which entries are returned may be different than before. If you need a specific order, you must sort the collection entries yourself. Except for blog entries which are already being sorted by date, we explicitly sort lexicographically (using [`localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) because [array sort wants a comparator function that returns negative, 0 or positive to indicate order](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
1 parent 0fc8243 commit 1b90683

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

src/components/pages/download/DownloadAccordion.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { getCollection, render } from 'astro:content';
44
const { family = 'nix' } = Astro.props;
55
const accordionId = `download-${family}-accordion`;
66
7-
let downloadOptions = (await getCollection('download')).filter(
8-
(option) => option.data.family === family,
9-
);
7+
let downloadOptions = (await getCollection('download'))
8+
.sort((a, b) => a.id.localeCompare(b.id))
9+
.filter((option) => option.data.family === family);
1010
---
1111

1212
<div id={accordionId} class="download-accordion">

src/pages/community.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import Layout from '../layouts/Layout.astro';
1616
const meetups = await getEntry('community', 'meetups');
1717
const teams = await getCollection('teams');
1818
19+
teams.sort((a, b) => a.id.localeCompare(b.id));
20+
1921
const nixcons = [
2022
{
2123
title: 'NixCon 2024 - Berlin',

src/pages/index.astro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import InlineSVG from '../components/util/InlineSVG.astro';
1313
const landingFeatures = await getCollection('landingFeatures');
1414
const demos = await getEntry('landing', 'demos');
1515
const posts = await getCollection('blog');
16+
17+
landingFeatures.sort((a, b) => a.id.localeCompare(b.id));
18+
1619
posts
1720
.sort((a, b) => {
1821
const dateA = new Date(a.data.date);

src/pages/learn.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import InlineSVG from '../components/util/InlineSVG.astro';
99
import Layout from '../layouts/Layout.astro';
1010
const learningManuals = await getCollection('learningManuals');
1111
12+
learningManuals.sort((a, b) => a.id.localeCompare(b.id));
13+
1214
const learningFeatures = [
1315
{
1416
imgUrl: '/src/assets/image/learn/installGfx.svg',

0 commit comments

Comments
 (0)