I have a list of dictionary of the previous 12 months values. I would like to sort it based on year and then month, without importing any extra libraries.
Current Input
[{'key': '2021 Mar', 'value': '101.239'},
{'key': '2021 May', 'value': '101.883'},
{'key': '2021 Sep', 'value': '102.657'},
{'key': '2021 Oct', 'value': '102.95'},
{'key': '2021 Nov', 'value': '103.959'},
{'key': '2022 Feb', 'value': '105.379'},
{'key': '2022 Apr', 'value': '106.547'},
{'key': '2022 Jan', 'value': '104.472'},
{'key': '2022 Jul', 'value': '108.836'},
{'key': '2022 Mar', 'value': '106.691'},
{'key': '2022 May', 'value': '107.598'},
{'key': '2022 Jun', 'value': '108.671'}]
Desired Output: In ascending order by Year and then Month
[{'key': '2021 Mar', 'value': '101.239'}, ......, {'key': '2022 Jul', 'value': '108.836'}]
I tried to re-create a new list-dictionary with Year and Month keys. From there, how do I proceed?
data_12= [{'key': '2021 Mar', 'value': '101.239'}, {'key': '2021 May', 'value': '101.883'}, {'key': '2021 Sep', 'value': '102.657'}, {'key': '2021 Oct', 'value': '102.95'}, {'key': '2021 Nov', 'value': '103.959'}, {'key': '2022 Feb', 'value': '105.379'}, {'key': '2022 Apr', 'value': '106.547'}, {'key': '2022 Jan', 'value': '104.472'}, {'key': '2022 Jul', 'value': '108.836'}{'key': '2022 Mar', 'value': '106.691'}, {'key': '2022 May', 'value': '107.598'}, {'key': '2022 Jun', 'value': '108.671'}, ]
data_12_new=[]
data_12_dic={}
for i in data_12:
data_12_dic['key']=i['key']
data_12_dic['value']=i['value']
data_12_dic['yr']= i['key'][0:4]
data_12_dic['mth']= i['key'][5:8]
data_12_new.append(data_12_dic)
#sort based on 1 key
newlist = sorted(data_12, key=lambda d: d['mth'])
# How to sort based on "yr" and "mth"?