Reputation: 8704
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?
Upvotes: 3
Views: 6111
Reputation: 164673
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
.
Upvotes: 4