Sorting a List of objects by attributes in python
# use lambda to define a comparison function for sort to use when comparing two items
somelist.sort(cmp = lambda x, y: cmp(x.resultType, y.resultType))
-----------------------
# To reverse sort the list in place...
ut.sort(key=lambda x: x.count, reverse=True)
# To return a new list, use the sorted() built-in function...
newlist = sorted(ut, key=lambda x: x.count, reverse=True)
------------------------
from operator import attrgetter
ut.sort(key = attrgetter('count'), reverse = True)
-------------------------
No comments:
Post a Comment