方法一:
class Myobj:
def __init__(self,id,score):
self.id=id
self.score=score
if __name__ == '__main__':
array=[Myobj(1,5),Myobj(2,2),Myobj(3,0)]
array=sorted(array,key=lambda x:x.score)
for obj in array:
print(obj.id,obj.score)
方法二:
class Myobj:
def __init__(self,id,score):
self.id=id
self.score=score
def __lt__(self, other):
return self.score < other.score
if __name__ == '__main__':
array=[Myobj(1,5),Myobj(2,2),Myobj(3,0)]
array=sorted(array)
for obj in array:
print(obj.id,obj.score)