Home > Software engineering >  Sorting dictionary by value by datetime in python
Sorting dictionary by value by datetime in python

Time:02-06

How do I sort a dictionary by value in datetime in shape of this:

{'user_0': [['item_805696', '2021-02-11 13:03:42'],
  ['item_386903', '2021-02-11 13:03:52'],
  ['item_3832', '2021-02-11 13:04:07'],
  ['item_849824', '2021-02-11 13:05:04'],
'user_1': [['item_97057', '2021-02-11 13:03:42'],
  ['item_644971', '2021-02-11 13:09:32'],
  ['item_947129', '2021-02-11 13:15:27'],
  ['item_58840', '2021-02-11 13:16:11'],
  ['item_640213', '2021-02-11 13:17:40'],
...

Im trying to sort values by datetime of second value in values of the dictionary

CodePudding user response:

You can follow this answer to convert the string representing the date to a number of seconds since the Unix epoch (1st January 1970): How to convert python timestamp string to epoch?

Then use can use the key argument of the sorted built-in method to use it to sort the dictionary how you want.

CodePudding user response:

You can give a key to tell list.sort or sorted how to sort.

A key to sort by the first value is lambda x: x[0]; a key to sort by the second value is lambda x: x[1].

d = {'user_0': [['item_805696', '2021-02-11 13:03:42'],
                ['item_849824', '2021-02-11 13:05:04'],
                ['item_386903', '2021-02-11 13:03:52'],
                ['item_3832',   '2021-02-11 13:04:07']],
     'user_1': [['item_58840',  '2021-02-11 13:16:11'],
                ['item_947129', '2021-02-11 13:15:27'],
                ['item_97057',  '2021-02-11 13:03:42'],
                ['item_640213', '2021-02-11 13:17:40'],
                ['item_644971', '2021-02-11 13:09:32']]}

for v in d.values():
    v.sort(key=lambda x: x[1])

print(d)
# {'user_0': [['item_805696', '2021-02-11 13:03:42'],
#             ['item_386903', '2021-02-11 13:03:52'],
#             ['item_3832',   '2021-02-11 13:04:07'],
#             ['item_849824', '2021-02-11 13:05:04']],
#  'user_1': [['item_97057',  '2021-02-11 13:03:42'],
#             ['item_644971', '2021-02-11 13:09:32'],
#             ['item_947129', '2021-02-11 13:15:27'],
#             ['item_58840',  '2021-02-11 13:16:11'],
#             ['item_640213', '2021-02-11 13:17:40']]}

Note that this only works because the timestamps are presented in the very practical format 'yyyy-mm-dd HH:MM:SS', and strings are sorted lexicographically, i.e., from left to right. If the timestamps had been in a less friendly format, such as 'dd-mm-yyyy HH:MM:SS', then you'd need to use the key to parse the timestamps.

CodePudding user response:

You can also use the datetime module in python standard librairy and here is the code

from datetime import datetime

d = {'user_0': [['item_805696', '2021-02-11 13:03:42'],
  ['item_386903', '2021-02-11 13:03:52'],
  ['item_3832', '2021-02-11 13:04:07'],
  ['item_849824', '2021-02-11 13:05:04']],
'user_1': [['item_97057', '2021-02-11 13:03:42'],
  ['item_644971', '2021-02-11 13:09:32'],
  ['item_947129', '2021-02-11 13:15:27'],
  ['item_58840', '2021-02-11 13:16:11'],
  ['item_640213', '2021-02-11 13:17:40']]}

def sort_by_datetime(val):
    return datetime.strptime(val[1], '%Y-%m-%d %H:%M:%S')

sorted_dict = {k: sorted(v, key=sort_by_datetime) for k, v in d.items()}
  • Related