Is it possible to sort on an attribute that is optional? For example, If I have a list like this,
{% set list = [{ "name": "John", "id": 1}, { "name": "", "id": 2}] %}
Is it possible to sort the list above by name? Using |sort(False, False, ‘name’) doesn’t seem to work. How do I achieve sorting the list above?
@jakwevagbe
Are you looping through them?
I tested and it seems to sort as expected. (the name that is null is returned first in order)
My test code:
{% set list = [{ "name": "John", "id": 1}, { "name": "", "id": 2}, { "name": "will", "id": 8}, { "name": "ared", "id": 9}] %}
{% for x in list|sort(False, False, "name") %}
{{ loop.index }} + {{ x.name }} + {{ x.id }}<br>
{% endfor %}
It doesn’t work for me. It returns empty. The list is the result of a HubDb query and is actually in the form of
[{id=38678890961, name=''}, {id=38678890962, name='Jane'}]
@alyssamwilie ,
Any additional thoughts here?
@jakwevagbe , could you drop in the code you are using?