排序

来源:互联网 发布:html javascript教程 编辑:程序博客网 时间:2024/06/14 21:25

class person:

def __init__(self,name=None,age=None):     self.name=name     self.age=age def __str__(self):      return '我是{0},今年{1}岁'.format(self.name,self.age) def __add__(self, other):     return person(self.name+other.name,self.age+other.age) def __cmp__(self, other):     return self.name # def __lt__(self,other): #     if self.name==other.name: #         return other.age<self.age #     else: #        return self.name.encode('gbk')<other.name.encode('gbk')

print(‘——————————-‘)
ps=[person(‘asdf’,10),person(‘zhangsan’,19),]
for p1 in ps:
print(p1)
print(‘——————————’)
for p1 in sorted(ps,key=lambda a:(a.name.encode(‘gbk’),-a.age)):
print(p1)

原创粉丝点击