What is the preferred way to sort/compare custom objects?

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


I just discovered the wiki page on sorting (http://wiki.python.org/moin/HowTo/Sorting/).  This describes the new way of sorting a container instead of using the cmp function.  But what do I do for custom objects?  If I write __lt__, __gt__, etc. functions for my objects, will these be used?  Is this better than defining a key for sorting my custom objects?  

Thanks,
Jeremy



More information about the Python-list mailing list