frontmatter
sortByFrontmatterDataKey
Sorts an Astro collection by a specified frontmatter key.
Supports:
- Case-insensitive, locale-aware comparison for string values
- Basic value comparison for numbers, dates, etc.
- Optional descending order
@template T - The shape of the collection frontmatter
@param {CollectionEntry
@param {string} dataKey - Key in frontmatter to sort by
@param {boolean} [desc=false] - If true, sort in descending order
returns {CollectionEntry
Usage:
const sorted = sortByFrontmatterDataKey(articles, "title");const sortedDesc = sortByFrontmatterDataKey(articles, "date", true);