Home > database >  Sort string that contain date in Python
Sort string that contain date in Python

Time:05-31

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)

CodePudding user response:

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.

CodePudding user response:

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']
  • Related