Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom sorting for collections #2723

Merged
merged 3 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions _includes/documents-collection.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{% assign entries = site[include.collection] %}

{% if include.sort_by == 'title' %}
{% if include.sort_by %}
{% if include.sort_order == 'reverse' %}
{% assign entries = entries | sort: 'title' | reverse %}
{% assign entries = entries | sort: include.sort_by | reverse %}
{% else %}
{% assign entries = entries | sort: 'title' %}
{% assign entries = entries | sort: include.sort_by %}
{% endif %}
{% elsif include.sort_by == 'date' %}
{% else %}
{% if include.sort_order == 'reverse' %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're doing repeated work here - we only need to check for sort_order once, after the sort_by check. Would you help fix this as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to be able to use the sort_by parameter even if sort_order is not specified?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be exact, here's what we have for now:

if (sort_by is given) {
  if (reverse) {
    entries = entries | sort_by | reverse;
  } else {
    entries = entries | sort_by;
  }
} else {
  if (reverse) {
    entries = entries | reverse;
  }
}

And here's what I'm expecting:

if (sort_by is given) {
  entries = entries | sort_by;
}

if (reverse) {
  entries = entries | reverse;
}

Clearly the latter looks more concise, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I have made the changes

{% assign entries = entries | sort: 'date' | reverse %}
{% else %}
{% assign entries = entries | sort: 'date' %}
{% assign entries = entries | reverse %}
{% endif %}
{% endif %}

Expand Down
7 changes: 6 additions & 1 deletion docs/_docs/10-layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ This layout displays all documents grouped by a specific collection. It accommod
collection: # collection name
entries_layout: # list (default), grid
show_excerpts: # true (default), false
sort_by: # date (default) title
sort_by: # date (default), title or any metadata key added to the collection's documents
sort_order: # forward (default), reverse
```

Expand All @@ -263,6 +263,11 @@ collection: recipes
```

If you want to sort the collection by title add `sort_by: title`. If you want reverse sorting, add `sort_order: reverse`.
You can also use any metadata key that is present in the documents. For example, you can add `number: <any number>` to your documents and use `number` as the sort key:

```yaml
sort_by: number
```

### `layout: category`

Expand Down