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

Jinja2 dictsort method accepts a reverse keyword #368

Closed
phyng opened this issue Sep 12, 2014 · 2 comments
Closed

Jinja2 dictsort method accepts a reverse keyword #368

phyng opened this issue Sep 12, 2014 · 2 comments

Comments

@phyng
Copy link

phyng commented Sep 12, 2014

Before:

dictsort(value, case_sensitive=False, by='key')

After:

dictsort(value, case_sensitive=False, by='key', reverse=False):

Why not Jinja2 dictsort method support reverse keyword?
Some guy gave the code: http://www.ifnamemain.com/posts/2014/Jul/04/jinja_pelican_dict_sort/

@Naddiseo
Copy link
Contributor

I think you could use the reverse filter for that.

@phyng
Copy link
Author

phyng commented Sep 12, 2014

from jinja2 import Template

d = {"a":2, "d":1, "c":4, "b":3}
print Template("{{ d|dictsort }}").render(d=d)
print Template("{{ d|dictsort(false, by='value') }}").render(d=d)
s = """
{% for i in d|dictsort(false, by='value')|reverse %}
{{ i }}
{% endfor %}
"""
print Template(s).render(d=d)

#output:
[('a', 2), ('b', 3), ('c', 4), ('d', 1)]
[('d', 1), ('a', 2), ('b', 3), ('c', 4)]

('c', 4)
('b', 3)
('a', 2)
('d', 1)

@Naddiseo Thank you! It works.

@phyng phyng closed this as completed Sep 12, 2014
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants