I want to be able to sort a list of classes by one attribute, which happens to be another class Date. I have done some research and want to use the sorted(list, key=lambda x:date)
method of sorting, but seeing as date is a class in itself how can I write an __lt__
function in date to allow me to sort chronologically?
I want something along the lines of:
if self.year!= other.year:
return self.year < other.year
elif self.month != pther.month
...
and so on.
Here is my Date class:
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
I should probably mention that this is the first time I have ever used Python, so I'm not very good at this.
Thanks in advance!