You can loop through a Jekyll collection sorted by a field using a combination of site.collections, sort, and a for loop in your Liquid templates. Here's a breakdown of how to do it, along with explanations and examples:
1. Understanding the Components
site.collections: This is a hash (dictionary) that contains all your collections. The keys are the collection names, and the values are the collection objects.
collection.docs: Within each collection object, docs is an array containing all the documents (posts/pages) belonging to that collection.
sort filter: Liquid's sort filter allows you to sort an array based on a specified property. You can sort in ascending or descending order.
for loop: The standard Liquid for loop iterates through the elements of an array.
2. Basic Example: Sorting by date (Default)
If you want to sort by the default date field (which is usually what you want for blog posts or chronological content), it's very straightforward:
{% assign my_collection = site.collections.my_collection %}: This assigns the collection named "my_collection" to a variable for easier use. Replace "my_collection" with the actual name of your collection.
my_collection.docs: This accesses the array of documents within the collection.
| sort: 'date': This pipes the docs array to the sort filter, sorting it by the date field in ascending order (oldest to newest).
{% for doc in ... %}: This starts a for loop that iterates through each document (doc) in the sorted array.
{{ doc.url }}: This outputs the URL of the current document.
{{ doc.title }}: This outputs the title of the current document.
{{ doc.date | date: "%Y-%m-%d" }}: This outputs the date of the current document, formatted as YYYY-MM-DD.
3. Sorting by a Custom Field
If you want to sort by a field other than date, you need to specify the field name in the sort filter. For example, if you have a field called priority (which might be a number) in your collection documents:
Jekyll's Liquid doesn't have a built-in way to sort by multiple fields directly. However, you can achieve this using a workaround with a custom filter or by pre-sorting the data in your YAML/Markdown files (which is often the best approach if the sorting is complex).
Workaround (Custom Filter - Not Recommended for Simple Cases):
You could create a custom Liquid filter to handle multi-field sorting. This is more complex and generally not necessary for simple sorting needs.
Pre-Sorting in YAML/Markdown (Recommended):
The most reliable and often easiest approach is to ensure your data is already sorted in the YAML or Markdown files that define your collection. This is especially true if the sorting logic is complex. Jekyll will then iterate through the documents in the order they are defined in the files.
6. Complete Example with Collection Definition
Let's say you have a collection named products with documents like this (in _data/products.yml):
Data Types: Make sure the field you're sorting by has a consistent data type (e.g., all numbers, all strings). Inconsistent data types can lead to unexpected sorting results.
String Sorting: When sorting strings, Liquid uses lexicographical (alphabetical) order.
Performance: For very large collections, sorting in Liquid can be slow. Consider pre-sorting the data if performance is critical.
Collection Name: Double-check that you're using the correct collection name in site.collections.your_collection_name.
YAML/Markdown Structure: Ensure your YAML or Markdown files are correctly formatted for Jekyll to parse them as a collection.
By following these steps and adapting the examples to your specific collection and field names, you can effectively sort and loop through your Jekyll collections in your templates. Remember to choose the method that best suits the complexity of your sorting requirements and the size of your collection. Pre-sorting in your data files is often the most robust and efficient solution.