Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

Miki miki.tebeka at gmail.com
Sun Sep 9 02:34:45 EDT 2007


>   steps.sort(key = lambda s: s.time)
This is why attrgetter in the operator module was invented.
from operator import attrgetter
...
steps.sort(key=attrgettr("time"))

HTH,
--
Miki <miki.tebeka at gmail.com>
http://pythonwise.blogspot.com




More information about the Python-list mailing list