>>> Student.__lt__ = lambda self, other:self.age <other .age >>> sorted(student_objects) [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 

• Key functions need not depend directly on the objects being sorted. A key function can also access external resources. For instance, if the student grades are stored in a dictionary, they can be used to sort a separate list of student names: 

>>> students=['dave','john','jane'] >>> newgrades ={ 'john':'F ','jane':'A','dave':'C'} >>> sorted(students, key =newgrades.__getitem__ ) ['jane', 'dave', 'john'] 

