Trouble sorting a list of objects by attributes

Stephen Hansen apt.shansen at
Fri Feb 6 17:41:25 EST 2009

> I think there may have been a misunderstanding.  I was already using
> attrgetter, my problem is that it doesn't appear to be sorting by the
> argument i give it.  How does sort work with strings?  How about with
> datetime.time or

You were using the attrgetter, but it looks like you weren't doing it correctly:


You don't specify a type, be it string or date or anything: you
specify the /name/ of the attribute to get. It'll handle sorting by
any data type that has an order to it without you having to worry
about it at all. Strings, ints, dates, times.

It should be:


If you want to sort by the value of the "department" attribute, which
happens to be a string.
If you want to sort by the "date" attribute, which happens to be a, you do:

Where date is not a datatype a la, but the name of an
attribute on your TimeSheet instances.


More information about the Python-list mailing list