Preferred method of sorting/comparing custom objects

Jeremy jlconlin at gmail.com
Thu Feb 24 11:41:51 EST 2011


I recently found the wiki page on sorting (http://wiki.python.org/moin/HowTo/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



More information about the Python-list mailing list