Sorting list of objects on arbitrary attribute

Alex Martelli aleax at aleax.it
Fri Jul 19 07:18:21 EDT 2002


Max M wrote:
        ...
> made my own from scratch and will use that in my code ;-)
        ...
>      def __call__(self, obj1, obj2):
>          return cmp(self._attrList(obj1), self._attrList(obj2))
        ...
>     aList.sort(sorter)

I hope you only sort *SHORT* lists, because performance
IS going to be lousy with this approach -- see the Searching
and Sorting chapter in the Python Cookbook, or the several
examples in the online Python cookbook of the DSU idiom,
and do your own timing with pretty modest lists -- you'll see.


Alex




More information about the Python-list mailing list