Use dateutil's relativedelta to sort an arbitrary list of Django objects by date

1
2
3
4
5
6
7
8
9
"""
Given a list of objects with date_time attributes
 that return a datetime, this sorts them in ascending 
order.  It requires the dateutil package from
 http://cheeseshop.python.org/pypi/python-dateutil"""

from dateutil.relativedelta import *

shows.sort(cmp=lambda x,y:relativedelta(x.date_time,y.date_time).minutes)

More like this

  1. Generic object_detail view with multiple named URL filters by cotton 1 month, 3 weeks ago
  2. sort_by_id_sequence by simon 3 years, 8 months ago
  3. Find nearby objects by paulsmith 4 years, 9 months ago
  4. Filter change list by a date range by aruseni 2 days, 2 hours ago
  5. TaggedManager and TaggedQuerySet with chainable tagged() methods implemented with django-tagging by fish2000 1 year, 11 months ago

Comments

(Forgotten your password?)