This code sorting dates without any issues. If I add any string to the my_dates variable can we sort similiarly? Splitting the string is the only way or any other alternative?
eg: my_dates = ['a_05112018.zip', 'a_25032017.zip', 'a_01112018.zip', 'a_07012017.zip']
from datetime import datetime
import re
pattern='Anyfile_<ddmmyyyy>.zip'
my_dates = ['05112018', '25032017', '01112018', '07012017']
result = re.search("<([dmy]{8,})>", pattern, flags=re.IGNORECASE)
if result:
date_pattern = result.group(1)
date_pattern = re.sub("dd", "%d", date_pattern, flags=re.IGNORECASE)
date_pattern = re.sub("mmm", "%b", date_pattern, flags=re.IGNORECASE)
date_pattern = re.sub("mm", "%m", date_pattern, flags=re.IGNORECASE)
date_pattern = re.sub("yyyy", "%Y", date_pattern, flags=re.IGNORECASE)
my_dates.sort(key=lambda date: datetime.strptime(date, date_pattern))
print(my_dates)