所以我有一个派生自dict的Person类,它包含诸如名字,姓氏和id之类的细节。
我将数据从json文件加载到json中的每个人的对象和实例化人员类(大约30个人),并将它们放在以下代码中的peopleList列表中:
import json
class Person(dict):
def __init__(self, firstname, lastname, personID):
super().__init__()
self.firstname = firstname
self.lastname = lastname
self.personID = personID
peopleList = []
with open('data.json', 'r') as peopleData:
dataObject = json.load(peopleData)
for registrant in dataObject['registrants']:
newperson = Person(registrant["firstname"], registrant["lastname"], registrant["personID"])
peopleList.append(newperson)

现在我尝试编写一个带有peopleList和criteria参数的函数。此函数将根据条件(名字或姓氏,甚至ID)按字母顺序对列表进行排序
尝试使用peopleList.sort()menthod并对此失败。你们的任何意见都会在这里受到高度赞赏。 :)