I need to sort my list comprised of variables created through a class by their name (ex. var.name).
I have tried the .sort(key=len, reverse=True) method but it returns this error:
Traceback (most recent call last):
File "/home/runner/TPF-Tests/main.py", line 92, in <module>
newvar=shop(1,possibleplace, 70, inventory)
File "/home/runner/TPF-Tests/main.py", line 64, in shop
itemlist.sort(key=len, reverse=True)
TypeError: object of type 'item' has no len()
I am a beginner so explanations would be greatly appreciated.
Your sort key (
key=len) doesn't make any sense. You need to sort on the length of theitem.nameattribute:For example: