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

Add sort_by to sort posts/pages #1802

Closed
haacked opened this issue Dec 10, 2013 · 12 comments · Fixed by #1849
Closed

Add sort_by to sort posts/pages #1802

haacked opened this issue Dec 10, 2013 · 12 comments · Fixed by #1849

Comments

@haacked
Copy link

haacked commented Dec 10, 2013

Related to #1735

Proposed syntax:

{% for group in site.pages.sort_by(:someProperty, 'asc') %}
   <h2>{{ group.Name }}</h2>
   <ul>
   {% for item in group.Items %}
      <li>{{ item.FooBar }}</li>
    {% endfor %}
  </ul>
{% endfor %}

If you don't specify 'asc' or 'desc' then it should probably do it 'desc' by default?

Also, I found a proposed :order keyword here: http://stackoverflow.com/questions/9218769/order-an-array-with-jekyll-liquid-template

Seems to me that chaining methods makes more sense to be consistent with the other operations, but I don't feel too strongly either way.

One thing to consider is multiple sorts:

site.pages.sort_by(:someProperty, 'asc').then_by(:anotherProperty, 'desc');

But I leave it to you to sort out the rubyisms. (pun intended).

@parkr
Copy link
Member

parkr commented Dec 10, 2013

I think it'd be smarter to write the sort_by filter such that it just sorts, and people can use the group_by (#1788) filter to group things. So to achieve what you want above, it'd look something like this:

{% for group in site.pages | group_by: "category" | sort_by: "name" %}
   <h2>{{ group.name }}</h2>
   <ul>
   {% for page in group.items %}
      <li>{{ page.title }}</li>
    {% endfor %}
  </ul>
{% endfor %}

@haacked
Copy link
Author

haacked commented Dec 10, 2013

How would you sort by category, then date, then author?

@nikmd23
Copy link

nikmd23 commented Dec 10, 2013

It'd be great to be able to pick the property to sort on for a "complex object".

An example, taken from the ossPerks.com front-matter, which sorts on a perk's name:

{% for item in page.perks | sort_by: "perk.name" %}
    {% include perk.html %}
{% endfor %}

Another alternative (not as nice) would be to annotate the property to sort on in the YAML:

perks:
- perk: {name: !!sort Crowdin, uri: 'http://crowdin.net/pricing'}
  description: Translation and localization management platform that handles both documents and software projects.
  categories: [Translation, Localization]
  company: {name: Crowdin, uri: 'http://crowdin.net/'}

- perk: {name: !!sort SSL Certificate, uri: 'http://www.godaddy.com/ssl/ssl-open-source.aspx'}
  description: One year free SSL Certificate from GoDaddy.
  categories: [TLS, SSL, Infrastructure]
  company: {name: GoDaddy, uri: 'http://www.godaddy.com/'}

which would simplify the syntax used in the for statement:

{% for item in page.perks | sort_by: "perk" %}
    {% include perk.html %}
{% endfor %}

@nikmd23
Copy link

nikmd23 commented Dec 10, 2013

I agree with @haacked that multi-level sorting is important, but I don't care much about the syntax to make it work.

@parkr
Copy link
Member

parkr commented Dec 10, 2013

@haacked Does the Liquid sort tag work for this?

sort (input, property = nil)

Sort elements of the array provide optional property with which to sort an array of hashes or drops.

Example:

{% assign variants = product.variants | sort: 'price' %}
{% for variant in variants %}

  • {{ variant.title }} - {{ variant.price | money }}
  • {% endfor %}

    From: http://docs.shopify.com/themes/liquid-basics/output#manipulation

    @haacked
    Copy link
    Author

    haacked commented Dec 11, 2013

    Interesting. I don't see any way to specify 'ascending' or 'descending'.

    @parkr
    Copy link
    Member

    parkr commented Dec 11, 2013

    @haacked Right, because they also have the reverse filter :)

    {{ my_array | sort | reverse }}

    @haacked
    Copy link
    Author

    haacked commented Dec 12, 2013

    Ahh! Ok, I'll close this. :)

    @haacked haacked closed this as completed Dec 12, 2013
    @afeld
    Copy link
    Contributor

    afeld commented Dec 18, 2013

    Does the Liquid sort tag work for this?

    Note that you can only sort site.pages by the attributes listed here. Less than ideal 😐

    @haacked
    Copy link
    Author

    haacked commented Dec 18, 2013

    Oh snap! That is very limiting. I should be able to sort by any property of page including custom fields. So is this an issue for liquid then?

    @afeld
    Copy link
    Contributor

    afeld commented Dec 18, 2013

    I'll take a crack at it.

    @parkr
    Copy link
    Member

    parkr commented Dec 18, 2013

    @afeld: This should help: #1829

    @jekyll jekyll locked and limited conversation to collaborators Feb 27, 2017
    Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
    Projects
    None yet
    Development

    Successfully merging a pull request may close this issue.

    5 participants