mvi*_*cia 7 python sorting dictionary jinja2
我该如何排序像这样的词典
my_dict = {
'abc': {'name': 'B', 'is_sth': True},
'xyz': {'name': 'A', 'is_sth': True}
}
Run Code Online (Sandbox Code Playgroud)
通过name在神社?
我已经尝试{% for id, data in my_dict|dictsort(by='value') if data.is_sth %}但是没有像我期望的那样工作.
解:
my_dict.items()|sort(attribute='1.name')
小智 5
jinja2 有一个完美的解决方案
do_dictsort(值, case_sensitive=False, by='key')
{% for item in mydict|dictsort %}
sort the dict by key, case insensitive
{% for item in mydict|dicsort(true) %}
sort the dict by key, case sensitive
{% for item in mydict|dictsort(false, 'value') %}
sort the dict by key, case insensitive, sorted
normally and ordered by value.
Run Code Online (Sandbox Code Playgroud)