4
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?

Tom
  • 71
  • 1
  • 1
  • 4

2 Answers2

10

This should work

import time
mydict.sort(key=lambda x:time.mktime(time.strptime(x['Timestamp'], '%d/%m/%Y %H:%M:%S')))
agamagarwal
  • 978
  • 7
  • 17
3
mydict.sort(key=lambda x:x['Timestamp'])

This will compare the elements of mydict based on their time stamp and sort it that way. Now, if you want to sort it by the actual time, then you have to convert that timestamp string to a Time object of some sort, and then sort mydict based on that. This question will likely help with that.

Community
  • 1
  • 1
El'endia Starman
  • 2,204
  • 21
  • 35