I am working on python2 environment (codes for legacy softwares) and I do not have access to numpy and pandas. I stumbled with a problem:
How to sort datetiems using month and days only (not the year, it’s for birthdates where only month and day matters).
My attempt
from datetime import date
lst = [date(2021,1,10), date(2020,3,1), date(2020,2,2), date(2020,2,1),date(2020,1,10)]
lst = sorted(lst)
print(lst)
[datetime.date(2020, 1, 10),
datetime.date(2020, 2, 1),
datetime.date(2020, 2, 2),
datetime.date(2020, 3, 1),
datetime.date(2021, 1, 10)]
Required
[datetime.date(2020, 1, 10),
datetime.date(2021, 1, 10)
datetime.date(2020, 2, 1),
datetime.date(2020, 2, 2),
datetime.date(2020, 3, 1),
]
>Solution :
you have to define a sorting key
sorted(lst, key=lambda x: (x.month, x.day))