小编gus*_*son的帖子

按属性排序对象列表

我试图在python中对对象列表进行排序,但是这段代码不起作用:

import datetime

class Day:
    def __init__(self, date, text):
        self.date = date
        self.text = text

    def __cmp__(self, other):
        return cmp(self.date, other.date)

mylist = [Day(datetime.date(2009, 01, 02), "Jan 2"), Day(datetime.date(2009, 01, 01), "Jan 1")]
print mylist
print mylist.sort()
Run Code Online (Sandbox Code Playgroud)

这个输出是:

[<__main__.Day instance at 0x519e0>, <__main__.Day instance at 0x51a08>]
None
Run Code Online (Sandbox Code Playgroud)

有人能告诉我解决这个问题的好方法吗?为什么sort()函数返回None

python

0
推荐指数
1
解决办法
602
查看次数

标签 统计

python ×1