-2

I want to sort a dictionary with key being month-year.

Eg: For

March 2017,January 2018,May 2017 etc.

My dictionary keys should be sorted in this order:

March 2017,May 2017,January 2018

What to do ?

Eg:

{'February 2017': {'a': 1.0, 'b': 683.01}, 
 'March 2018': {'a': 0.0, 'b': 623.79}, 
 'March 2017': {'a': 1.0, 'b': 683.01}}

NB: Values of Month Year comes dynamically.

vbt
  • 795
  • 7
  • 31
  • 2
    Use python3.7, or use `collections.OrderedDict` – rafaelc Aug 08 '18 at 11:56
  • @ukemi : In that question, all key values can easily be sorted. In my question,the values come dynamically and I have to sort it. Eg: One value will be in March 2017 and other will be in March 2018.How can I sort those ? – vbt Aug 08 '18 at 11:59
  • 1
    @RafaelC : It works.. Thanks mate.. – vbt Aug 08 '18 at 12:05
  • 1
    https://stackoverflow.com/questions/44861179/python-sort-ordereddict-keys-chronologically – iacob Aug 08 '18 at 12:08
  • 1
    Note that you can't store different values with the same key in python dict. – Lev Zakharov Aug 08 '18 at 12:08
  • @ukemi : https://stackoverflow.com/questions/44861179/python-sort-ordereddict-keys-chronologically yes this a possible duplicate . – vbt Aug 08 '18 at 12:12

3 Answers3

2

You need to convert the keys into dates so they will sort properly. Assuming your dictionary is called example_dict

import datetime
keys = list(example_dict.keys())
keys.sort(key=lambda x: datetime.datetime.strptime(x,'%B %Y'))
T Burgis
  • 1,395
  • 7
  • 9
1

You need to use special key function for sorting:

months = ['January',
          'February',
          'March',
          'April',
          'May',
          'June',
          'July',
          'August',
          'September',
          'October',
          'November',
          'December']

month_num = dict(zip(months, range(12)))


def compare(date):
    month, year = date.split(' ')
    return (year, month_num[month])


dates = {
    'February 2017': {'a': 1.0, 'b': 683.01},
    'March 2018': {'a': 0.0, 'b': 623.79}}

dict(sorted(dates.items(), key=lambda x: compare(x[0])))
Lev Zakharov
  • 2,409
  • 1
  • 10
  • 24
0

FYI: In , since dictionaries are 'unordered', so to speak, your dictionary in the original post is syntactically incorrect, since there are two keys with the same value. The interpreter will override one of them.

I presumed that one of them should be May 2018, as per your original post, so that is the dictionary I used for my answer:

{'February 2017': {'a': 1.0, 'b': 683.01}, 
 'May 2018': {'a': 0.0, 'b': 623.79}, 
 'March 2018': {'a': 1.0, 'b': 683.01}}

Since, as stated above, dictionaries are not ordered obviously (hash values), so we must convert to a list and then play around. A one liner solution is as follows:

sorted(list(data.items()), key=lambda x: [x[0].split()[-1], x[0].split()[0], x[1]])

You get the output as follows:

>>> sorted(list(data.items()), key=lambda x: [x[0].split()[-1], x[0].split()[0], x[1]])
[('February 2017', {'a': 1.0, 'b': 683.01}), ('March 2018', {'a': 1.0, 'b': 683.01}), ('May 2018', {'a': 0.0, 'b': 623.79})]
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76