我希望能够按一个属性对类列表进行排序,这恰好是另一个类 Date。我做了一些研究,想使用sorted(list, key=lambda x:date)排序方法,但是看到日期本身就是一个类,我如何__lt__在日期中编写一个函数来让我按时间顺序排序?
我想要一些类似的东西:
if self.year!= other.year:
return self.year < other.year
elif self.month != pther.month
...
Run Code Online (Sandbox Code Playgroud)
等等。
这是我的日期课程:
class Date:
def __init__(self, month, day, year, minute, hour, string):
self.month = month
self.day = day
self.year = year
self.minute = minute
self.hour = hour
self.string = string
Run Code Online (Sandbox Code Playgroud)
我应该提一下,这是我第一次使用 Python,所以我不太擅长这个。
提前致谢!