I created class named Student() with attributes name and surname. In that class I created class variable -> all_students = []
When creating an object, it is being added to that class variable = all_students.
How to sort that list of objects by the lenght of the attribute name from longest to shortest?
import operator
class Student:
all_students = []
def __init__(self, name, surname):
self.name = name
self.surname = surname
Student.all_students.append(self)
s_1 = Student("Jacobbbb", "anything")
s_2 = Student("Mat", "anything1")
s_3 = Student("Marcooooooo", "sss")
new_list = sorted(Student.all_students, key=operator.attrgetter(len('name')))
for student in new_list:
print(student.name)
I tried with operator, but I can't do that in this way. Will be gratefull for help.