0

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)
vai_007
  • 29
  • 5

2 Answers2

0

You don't need to split the text. The datetime module can handle string to date conversion pretty good. see: Converting string into datetime

text = 'a_25032017.zip'
datetime.strptime(text, 'a_%d%m%Y.zip') # This is valid
# datetime.datetime(2017, 3, 25, 0, 0)

Once you convert all of the list to DateTime objects you can sort them any way you want.

MSH
  • 1,743
  • 2
  • 14
  • 22
  • what if I don't know the suffix and prefix(it may a or b or any file) – vai_007 May 30 '22 at 21:40
  • unfortunately, I am aware of no wild card for `strptime`. But if there is always an `_` you may split the text first. A loop can handle that. – MSH May 30 '22 at 21:59
0

Try:

import re

my_dates = [
    "a_05112018.zip",
    "a_25032017.zip",
    "a_01112018.zip",
    "a_07012017.zip",
]


def sort_func(value):
    dd, mm, yyyy = re.search(r"_(\d{2})(\d{2})(\d{4})\.zip", value).groups()
    return yyyy, mm, dd


my_dates = sorted(my_dates, key=sort_func)
print(my_dates)

Prints:

['a_07012017.zip', 'a_25032017.zip', 'a_01112018.zip', 'a_05112018.zip']
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91