dictsortreversed Filter

Data Structure

Argument: key (required)


Reverse sorts a list of dictionaries by key.


foods = [
    {'name': 'Apple', 'category': 'Fruit'},
    {'name': 'Banana', 'category': 'Fruit'},
    {'name': 'Corn', 'category': 'Vegetable'},
    {'name': 'Grape', 'category': 'Fruit'},
    {'name': 'Hamburger', 'category': 'Meat'},
    {'name': 'Pepper', 'category': 'Vegetable'},


  {% for food in foods|dictsortreversed:'category' %}
    <li>{{ food.name }}, {{ food.category }} </li>
  {% endfor %}


    <li>Corn, Vegetable </li>
    <li>Pepper, Vegetable </li>
    <li>Hamburger, Meat </li>
    <li>Apple, Fruit </li>
    <li>Banana, Fruit </li>
    <li>Grape, Fruit </li>

Notice that the list is sorted by category in reverse order.

dictsortreversed can also be used to sort lists of other sequences using the index of the item you wish to sort by as the argument.

See also: dictsort.

Did we get something wrong? Is there a use case for the dictsortreversed filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST