I want to sort a list
of some objects by an attribute, but I also want to be able to specify a key
when doing the sort (of those attributes). I have seen answers like this, which use the attribute to sort the objects, but I want to allow for other logic of sorting besides (for strings) just alphabetizing. Here is an example class and sort function to show what I mean:
class Pet:
def __init__(self, name):
self.name = name
def __repr__(self): # for readability
return 'Pet({})'.format(self.name)
def sort_pets(pets, reverse=False):
pets.sort(reverse=reverse, key=lambda x : x.name)
So with this function, I can sort a list of Pet
objects by their name
:
>>> pets = [Pet(i) for i in ['Dexter Lewis Cumberbatch', 'Alice', 'Bozo Bob', 'Cy']]
>>> sort_pets(pets)
>>> print(pets)
[Pet(Alice), Pet(Bozo Bob), Pet(Cy), Pet(Dexter Lewis Cumberbatch)]
I can do so alphabetically or reverse alphabetically (using reverse
). But I want to have options for sorting the pets (by name) with different logic (like say the name length, number of spaces, last letter, etc.). I can make a key
parameter for sort_pets
, but I can't pass it to the key
parameter of sort
, because it is already being used to access the name
attribute. Is there anyway to accomplish this?