3

I have an object called Orders. One of the field in orders is last_modified. the format of this field is 01-JAN-16 02.15.49.086630 PM

Then i have a list of these orders i.e., orders_list. How can I sort this list based on last_modified value?

1 Answer 1

4

You can use sorted with a custom key. In this case, you need to convert your string field to datetime:

from datetime import datetime

date_format = '%d-%b-%y %H.%M.%S.%f %p'

res = sorted(orders_list, key=lambda x: datetime.strptime(x.last_modified, date_format))

See Python's strftime directives to see how to construct date_format.

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

Comments

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.