Sorting a List of Objects by an Attribute of the Objects Case-Insensitively
paddy3118 at googlemail.com
Wed Apr 9 06:58:45 CEST 2008
On Apr 9, 4:04 am, Jason <elgrandchig... at gmail.com> wrote:
> Hi folks--
> Basically, I have a pressing need for a combination of 5.2 "Sorting a
> List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects
> by an Attribute of the Objects" from the Python Cookbook.
> My first guess isn't working:
> import operator
> def sort_by_attr(seq, attr):
> return sorted(seq, key)
> ...would much appreciate any guidance!
Try key= lambda x: x.attr.lower()
The above should calculate the key only once for the items to be
sorted rather than using cmp which calculates more than that.
More information about the Python-list