Preferred method of sorting/comparing custom objects

Chris Rebert clp2 at
Thu Feb 24 12:16:56 EST 2011

On Thu, Feb 24, 2011 at 8:41 AM, Jeremy <jlconlin at> wrote:
> I recently found the wiki page on sorting (  This page describes the new key parameter to the sort and sorted functions.
> What about custom objects?  Can I just write __lt__, __gt__, etc. functions and not have to worry about the key parameter?  Is that the preferred (i.e., fastest) way to do things or should I use a lambda function similar to what is given as an example on the wiki page?
> For my custom objects, I would prefer to write the comparison functions as that seems easiest in my situation, but I would like to know what is the preferred/accepted way.
> Thanks,
> Jeremy

Please don't make duplicate posts to the mailinglist/newsgroup. If you
need to make corrections to a posting, do it as a follow-up to the
original post.

- Chris

More information about the Python-list mailing list