Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort timestamp in python dictionary

mydict = [{'Counted number': '26', 'Timestamp': '8/10/2015 13:07:38'},{'Counted number': '14','Timestamp': '8/10/2015 11:51:14'},{'Counted number': '28','Timestamp': '8/10/2015 13:06:27'}, {'Counted number': '20','Timestamp': '8/10/2015 12:53:42'}]

How to sort this dict based on timestamp?

like image 843
Tom Avatar asked Dec 08 '22 00:12

Tom


1 Answers

This should work

import time
mydict.sort(key=lambda x:time.mktime(time.strptime(x['Timestamp'], '%d/%m/%Y %H:%M:%S')))
like image 122
agamagarwal Avatar answered Dec 11 '22 11:12

agamagarwal