0

I have a list of strings in this format:

my_strings = [
"10.02.20 time 09.24",  
"06.02.20 time 08.51",  
"10.02.20 time 09.24",  
"10.02.20 time 09.25",  
"10.02.20 time 09.26",  
"10.02.20 time 09.27",  
"10.02.20 time 09.28",  
"10.02.20 time 09.29"   
]

How can I sort them based on date and time, both normally and reversely? I think I should use something like:

my_strings.sort(key=lambda date: datetime.strptime(date, "%d-%b-%y"))

But this doesn't work.

3 Answers 3

2

Assuming your format is "day.month.year time hour.minute", you need:

my_strings.sort(key=lambda date: datetime.strptime(date, '%d.%m.%y time %H.%M'))

If by "normally and reverse" you mean in ascending and descending order, for the latter you can either reverse the sorted list with my_strings[::-1], or directly while sorting with my_strings.sort(key=..., reverse=True).

Sign up to request clarification or add additional context in comments.

5 Comments

Did you try both methods to see if they came out with the same order? I suspect not.
Sorry, my mistake - it actually does work, but only because the test data is poorly chosen. Try changing the last item in my_strings to "10.02.19 time 09.29".
@MarkRansom Ah, this is a very good point! I tunnel visioned because most dates were the same and didn't think about the logic. String sorting would have worked for year-month-day-hour-minute order only.
Yes, string sorting can work well - if the parts are in the correct order, and always have the same number of digits. I'm a big believer in ISO 8601 applied to UTC times.
@Reti43 your solution worked for me. Thank you!
0

As per the documentation for the strptime function, the correct call for the function is:

strptime(date, "%d.%m.%y time %H.%M")

The %b you put in your code is for "Month as locale’s abbreviated names (Jan, Feb, …, Dec), but the correct is %m for "Month as a zero-padded decimal number", and you also need to include time %H.%M in the string declaring the format for it to work, or else you will get an ValueError: time data '10.02.20 time 09.24' does not match format '%d.%b.%y'

3 Comments

As for sorting, as @Reti43 said in its answer, a simple my_strings.sort() should work!
You can't sort by string unless the strings are in the proper format. And these aren't.
Thanks for the clarification!
-1

the statement you import into the datetime module may be import datetime, try from datetime import datetime. it is all code

from datetime import datetime

my_strings = [
    "10.02.20 time 09.24",
    "06.02.20 time 08.51",
    "10.02.20 time 09.24",
    "10.02.20 time 09.25",
    "10.02.20 time 09.26",
    "10.02.20 time 09.27",
    "10.02.20 time 09.28",
    "10.02.20 time 09.29"
]
my_strings.sort(key=lambda date: datetime.strptime(date, "%y.%m.%d time %H.%M"))
print(my_strings)

1 Comment

Pay attention to the order of the date parts.

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.