Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 2
    No problem. btw, if muhuk is right and it's a list of Django objects, you should consider his solution. However, for the general case of sorting objects, my solution is probably best practice. Commented Dec 31, 2008 at 17:12
  • 70
    On large lists you will get better performance using operator.attrgetter('count') as your key. This is just an optimized (lower level) form of the lambda function in this answer. Commented Dec 31, 2008 at 19:35
  • 8
    Thanks for the great answer. In case if it is a list of dictionaries and 'count' is one of its key then it needs to be changed like below : ut.sort(key=lambda x: x['count'], reverse=True) Commented Dec 8, 2016 at 21:20
  • 2
    I suppose it deserves the following update: if there is a need to sort by multiple fields, it could be achieved by consecutive calls to sort(), because python is using stable sort algorithm. Commented Feb 23, 2020 at 14:41
  • 2
    Thanks @KenanBanks, you were right. Annoyingly outlook was doing some weird things with calendar timezones so that some came through without the timezone details... no idea why! Commented Apr 20, 2022 at 6:05