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?

4
  • Why do you have these weird strings? What is the expected output for the given list? Commented Nov 14, 2018 at 16:33
  • sorry the original string had '<' characters in it which interfered with the way it was displayed. I have edited the question as you can see now Commented Nov 14, 2018 at 16:34
  • 1
    Avoid list as a variable name, there's already the builtin list. Commented Nov 14, 2018 at 16:51
  • yes list should not have been used as a variable name. thnx Commented Nov 14, 2018 at 17:07

3 Answers 3

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) 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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]))

1 Comment

I haven't used dateutil so far. But it seems promising. Will keep this in mind.
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']

2 Comments

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.
exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.