python - How do operator.itemgetter() and sort() work? - Stack Overflow

PHOTO EMBED

Sat Mar 26 2022 22:38:28 GMT+0000 (Coordinated Universal Time)

Saved by @Rajab #python

# initialize
a = []

# create the table (name, age, job)
a.append(["Nick", 30, "Doctor"])
a.append(["John",  8, "Student"])
a.append(["Paul", 22, "Car Dealer"])
a.append(["Mark", 66, "Retired"])    

# sort the table by age
import operator
a.sort(key=operator.itemgetter(1))    

# print the table
print(a)
content_copyCOPY

https://stackoverflow.com/questions/18595686/how-do-operator-itemgetter-and-sort-work