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

Sorting & ordering collections #2515

Closed
ahmedelgabri opened this Issue Jun 14, 2014 · 9 comments

Comments

Projects
None yet
8 participants
@ahmedelgabri

ahmedelgabri commented Jun 14, 2014

I'm trying to sort/order & limit collections.

collections:
  collection:
    output: true

now in my template I have this

{% for item in site.collection limit:6 | sort: 'date' %}
    <li>
        <a href="{{ item.url }}">
             <img src="{{ item.thumb }} " alt="">
            </figure>
            <p>{{ item.title }}</p>
        </a>
    </li>
{% endfor %}

It doesn't work, the ordering is always by file name. I tried even added order_page: n inside each page YAML & use this to with sort: 'order_page' but it didn't work. Tried using reversed to fix this but then limit: 6 returns the same 6 pages.

I want to order this collection but date "new pages first" & limit it showing the latest 6 pages.

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Jun 14, 2014

Member

Did you take a look at #2345?

Member

parkr commented Jun 14, 2014

Did you take a look at #2345?

@budparr

This comment has been minimized.

Show comment
Hide comment
@budparr

budparr Jun 14, 2014

Contributor

t's fixed for 2.1, which is not released yet, though you could run the master branch locally.

Contributor

budparr commented Jun 14, 2014

t's fixed for 2.1, which is not released yet, though you could run the master branch locally.

@parkr parkr closed this Jun 14, 2014

@ahmedelgabri

This comment has been minimized.

Show comment
Hide comment
@ahmedelgabri

ahmedelgabri Jun 14, 2014

@budparr Thanks will try running the master branch locally

ahmedelgabri commented Jun 14, 2014

@budparr Thanks will try running the master branch locally

@doktorbro

This comment has been minimized.

Show comment
Hide comment
@doktorbro

doktorbro Jun 15, 2014

Member

@ahmedelgabri You cannot sort inside the for-statement. The right way is:

{% assign items = site.collection | sort: 'date' %}
{% for item in items limit:6 %}
    <li>
        <a href="{{ item.url }}">
             <img src="{{ item.thumb }} " alt="">
            </figure>
            <p>{{ item.title }}</p>
        </a>
    </li>
{% endfor %}
Member

doktorbro commented Jun 15, 2014

@ahmedelgabri You cannot sort inside the for-statement. The right way is:

{% assign items = site.collection | sort: 'date' %}
{% for item in items limit:6 %}
    <li>
        <a href="{{ item.url }}">
             <img src="{{ item.thumb }} " alt="">
            </figure>
            <p>{{ item.title }}</p>
        </a>
    </li>
{% endfor %}
@ahmedelgabri

This comment has been minimized.

Show comment
Hide comment
@ahmedelgabri

ahmedelgabri commented Jun 15, 2014

@penibelst Thanks for this

@falkorichter

This comment has been minimized.

Show comment
Hide comment
@falkorichter

falkorichter Oct 7, 2015

Contributor
{% for item in site.collection reversed %}

inverts the order the collection

Contributor

falkorichter commented Oct 7, 2015

{% for item in site.collection reversed %}

inverts the order the collection

@jacobmischka

This comment has been minimized.

Show comment
Hide comment
@jacobmischka

jacobmischka Mar 7, 2016

@penibelst Thank you. Two years later this comment is still helpful.

jacobmischka commented Mar 7, 2016

@penibelst Thank you. Two years later this comment is still helpful.

@deanpanayotov

This comment has been minimized.

Show comment
Hide comment
@deanpanayotov

deanpanayotov May 7, 2018

@penibelst Thank you!
@jacobmischka Four years later that comment is still helpful.

deanpanayotov commented May 7, 2018

@penibelst Thank you!
@jacobmischka Four years later that comment is still helpful.

@merlinpatt

This comment has been minimized.

Show comment
Hide comment
@merlinpatt

merlinpatt May 19, 2018

Are there any plans to make sorting possible in the for loop? I'd rather not have an extra variable for something so simple

merlinpatt commented May 19, 2018

Are there any plans to make sorting possible in the for loop? I'd rather not have an extra variable for something so simple

@jekyll jekyll locked as resolved and limited conversation to collaborators May 19, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.