[英]Is it possible to sort a list by length in Jinja2?
我想按长度对列表进行排序,但是我不确定Jinja2是否可以:
{% for item in item_list | sort(length) %}
If item
is your custom class, you can override __lt__
and you'll be able to sort directly with the sort
filter. 如果
item
是您的自定义类,则可以覆盖__lt__
并且可以使用sort
过滤器直接进行sort
。 Since Jinja 2.6, you have a length
attribute on your item, you can do so with sort(attribute='length')
. 从Jinja 2.6开始,您在商品上具有
length
属性,可以使用sort(attribute='length')
。 Taken from the Jinja2 sort filter documentation 摘自Jinja2排序过滤器文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.