How to sort a dictionary based on different key fields?

Viewed 129

I have a dictionary which contains id,Direction,year,month,day,hour and I want to sort it based on month,day,hour. Below is my dictionary and the values it has:

table = {('2339', 'W', '2016', '6', '2', '11'): [0],
         ('2339', 'W', '2016', '1', '16', '8'): [0],
         ('2339', 'W', '2016', '5', '8', '22'): [2],
         ('2339', 'W', '2016', '1', '17', '3'): [0]}

and the code I used to sort:

    result_dict = sorted(table.items(), key=operator.itemgetter(0))

But this will not sort it the way I want. How can I sort it based on month,day, and hour. The result that I want is look like this:

 ID   Direction  year   month  day  hour
2339    W        2016   1       1    0
2339    W        2016   1       1    1
2339    W        2016   1       1    2
2339    W        2016   1       1    3
...     ..      ..      ..      ..   ..
3 Answers
Related