'Sorting arrays by date with datetime in python 3.10
I have tried all sorts of code but none of it works so far, this is as close as I have gotten all of them get a similar error
Heres the data i want to sort
{'15/2/2022': 'C:\\Users\\me\\filelocation\\Stuff\\codestuff\\Python\\PDF\\files\\paySlip15Febuary.pdf',
'18/1/2022':' C:\\Users\\me\\filelocation\\Stuff\\codestuff\\Python\\PDF\\files\\paySlip18January.pdf' }
and so on
This is the code i am using currently, but i have tried much more
def key(s):
fmt = "%d-%m-%Y"
s = ''.join(s.rsplit(':', 1)) # remove colon from offset
return datetime.strptime(s, fmt)
sorted(dates.values(), key=key)
No matter how many different things i try i get this error. Btw if you are wondering i am creating an array of dates from a list
raise ValueError("time data %r does not match format %r" %
Solution 1:[1]
Sorry guys here is the correct answer
from datetime import datetime
sorted = OrderedDict(sorted(dates.items(), key = lambda x:datetime.strptime(x[0], '%d/%m/%Y'), reverse=True))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
