[Solved]-How to sort a list of objects in a template in alphabetic order?

7👍

You’ll need to write a custom filter which sorts by lower case. It’s pretty simple:

@register.filter
def sort_lower(lst, key_name):
    return sorted(lst, key=lambda item: getattr(item, key_name).lower())

But if your list is a set of objects from the database, you shouldn’t really be sorting them in Python – you should get the database to return them in the order you want.

Edit

How are you using the filter? It should be exactly the same as the dictsort one: object_list|sort_lower:"name".

To sort the queryset in the database, you can use the extra method to add a lower-case version of the field:

MyModel.objects.all().extra(select={'lower_name': 'LOWER(NAME)'}, order_by='lower_name')

6👍

A very old question, but the issue just came up for me. Just add .lower to dictsort, e.g.:

{% for item in object_list|dictsort:"name.lower" %}

👤kdwarn

2👍

I know this is an old question, but the easiest way I see to sort a list case insensitive is:

name_of_list.sort(key=str.lower)

See link for reference:
https://www.afternerd.com/blog/python-sort-list/

You can test it using this code:

my_list = ["alice", "Bob", "eve", "Zoe"]
my_list.sort()
print("case sensitive list:")
print(my_list)

my_list.sort(key=str.lower)
print("case insensitive list:")
print(my_list)

Then when you display your list in templates the items will already be sorted.

👤Nikki

1👍

In fact, I looked at the original code of dictsort in .../lib/python2.7/site-packagesdjango/template/defaultfilters.py. And, I just added a customized cmp method:

@register.filter
def sort_lower(value, arg):
    try:
        return sorted(value, key=Variable(arg).resolve,
                      cmp=lambda x,y: cmp(x.lower(), y.lower()))
    except (TypeError, VariableDoesNotExist):
        return ''

This way allow to sort by subfields (e.g. field1.field2) which was needed in my case.

👤perror

Leave a comment