3
list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy' ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]

datet = re.compile(r'ResultDatetime:(\d{4}-\d{2}-\d{2} \d{2}:\d{2})')

list.sort(key = lambda x: ........)

I want to sort the lists in an order starting with the earliest date. How should I go about it using lambda and regex?

timgeb
  • 76,762
  • 20
  • 123
  • 145
dratoms
  • 159
  • 11

3 Answers3

2

With the code you have there it is sufficient to do:

list.sort(key=lambda x: datet.search(x).group(1))

(but please, don't use list as a variable name).

There is no need to convert the extracted string to a datetime as it is already in a format that will sort naturally.

Note however that if any string does not match the regex this will generate an error, so you may be better to split the key out into a named multi-line function and test for a successful match before returning the matched group.

def sort_key(line):                                                                                                                                               
    match = datet.search(line)                                                                                                                                               
    if match:                                                                                                                                                     
        return match.group(1)                                                                                                                                                    
    return ''        

data = [
    'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
    'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
    'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime'
]
data.sort(key=sort_key) 
Duncan
  • 92,073
  • 11
  • 122
  • 156
  • thanks for that syntax. that was elusive to me. and thanks for that neat little function there. Although the list element part is autogenerated and is unlikely that there will be missing values, your function is going to help me a lot in future, a newbie to python (and programming in general) that I am. – dratoms Nov 14 '18 at 17:13
0

You can use dateutil.parser.parse (see this answer: Parse date strings?) to parse the date and re.findall to get it from a string

import re     
from dateutil.parser import parse

list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]
datet = re.compile(r'ResultDatetime:(\d{4}-\d{2}-\d{2} \d{2}:\d{2})')

list.sort(key = lambda x : parse(re.findall(datet, x)[0]))
mrzasa
  • 22,895
  • 11
  • 56
  • 94
0

I think the simplest solution without any imports would be:

data  = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
         'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 
         'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']

sorted_data = sorted(data, key=lambda x: x[20:36])

print(sorted_data)

Output:

        ['xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 
         'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 
         'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']
Nick
  • 3,454
  • 6
  • 33
  • 56
  • The last string has the date at a slightly different offset. I think the OP's intention is that xxxx and yyyyy could be any arbitrarily long strings. – Duncan Nov 14 '18 at 17:08
  • exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here. – dratoms Nov 14 '18 at 17:20