I was able to sort a list of objects by a string attribute using this excellent post:
How to sort a list of objects based on an attribute of the objects?
However, I also need the sort to ignore case when alphabetizing. Suppose I had a list of objects that each had an attribute "Name":
class MyObject (object):
def __init__(self):
MyObject.Name = None
itemsList = []
object1 = MyObject()
object1.Name = "Orange"
itemsList.append(object1)
object2 = MyObject()
object2.Name = "apple"
itemsList.append(object2)
object3 = MyObject()
object3.Name = "Banana"
itemsList.append(object3)
I can sort them like this:
itemsList.sort(key=lambda item : item.Name)
But how do I also ignore case? I tried throwing lower
onto Name
at that last line but it didn't work for me.