Skip to content

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[]} collection - Array of Astro collection entries

@param {string} dataKey - Key in frontmatter to sort by

@param {boolean} [desc=false] - If true, sort in descending order

returns {CollectionEntry[]} - Sorted array (mutated in-place)

Usage:

const sorted = sortByFrontmatterDataKey(articles, "title");
const sortedDesc = sortByFrontmatterDataKey(articles, "date", true);