sorting - How to sort datemonthyear by reverse chronological order using Python -
i have folder named 29jun2011,12aug2013,31jan2013,08aug1985. have sort out reverse chronological order using python script. , have store latest 1 in variable , print it.
import datetime dt date_strs = [ "29jun2011", "08aug1985", "12aug2013", ] my_format = "%d%b%y" datetime_objs = [] date_str in date_strs: my_datetime = dt.datetime.strptime(date_str, my_format) datetime_objs.append(my_datetime) print datetime_objs datetime_objs.sort(reverse=true) print datetime_objs print datetime_objs[0].strftime("%b %d, %y") --output:-- [datetime.datetime(2011, 6, 29, 0, 0), datetime.datetime(1985, 8, 8, 0, 0), datetime.datetime(2013, 8, 12, 0, 0)] [datetime.datetime(2013, 8, 12, 0, 0), datetime.datetime(2011, 6, 29, 0, 0), datetime.datetime(1985, 8, 8, 0, 0)] august 12, 2013
Comments
Post a Comment