class Person:
def __init__(self, name, grade):
self._name=name
self._grade=grade
def __lt__(self,other):
return self._name<other._name
def __repr__(self):
return '{} {}'.format(self._name, self._grade)
class Student (Person):
def __init__(self, name, grade):
Person.__init__(self, name, grade)
def __lt__(self,other):
return self._Person_grade<other._Person_grade
studentdata=[]
L=['????; 90', 'Richard; 100', 'Lilia; 80', 'Alex; 95']
for student in L:
elements=student.split(';')
name=elements[0]
grade=int(elements[1])
student=Student(name, grade)
studentdata.append(student)
studentdata.sort()
print(studentdata)
Question:
#HELLO, I want to sort student data according to their names by using __lt__ method
#even if it is overrided in subclass
#So my expected output is: [Alex 95, Lilia 80, Richard 100, Xavi 90]
#But I get [Lilia 80, Xavi 90, Alex 95, Richard 100] because it sorts according to the grades.
#How can I do that? I think instead of using studentdata.sort() directly, we need to call __lt__ method of the class Person, but I dont know how to do that.