Skip to content Skip to sidebar Skip to footer

Typeerror: '<' Not Supported Between Instances - Objects

I am trying to sort by name but it is typing an Error: TypeError: '<' not supported between instances of 'Person' and 'Person'. Could you tell me where the problem is? Here is m

Solution 1:

How about writing a custom key function and pass it as sorted() argument?

sorted_list = sorted(person.mentees, key=lambda p: p.name)

Solution 2:

It's just what it says: you cannot sort Person objects. If you want this to work, you have to define at least the __lt__ operator for your class, using whatever sort criterion you had in mind -- perhaps alphabetical by name?

Another possibility is to simply write your own function, and call it with person.mentees.obj_sort.


Also, I'm not sure why this matters: you never use the return value of this operation. You store it in the local variable a (that's a poor variable name, by the way), and never use it.

Post a Comment for "Typeerror: '<' Not Supported Between Instances - Objects"