# from collections import OrderedDict # import json # # # courses = OrderedDict({ # 'a7': { # 'title': 'SEED Analog Learning 300 - August 2016', # 'start_date': '2016-08-15T00:00:00Z', # 'end_date': '2016-12-15T00:00:00Z', # 'enrollment_start': '2016-08-15T00:00:00Z', # 'enrollment_end': '2016-08-29T00:00:00Z', # 'expiration_date': '2017-03-15T00:00:00Z', # 'courseware_url': None, # 'courseware_id': 'course-v1:MITx+Analog+Learning+300+Aug_2016', # 'id': 22 # }, # # 'a9': { # 'title': 'SEED Digital Learning 100 - August 2020', # 'start_date': '2019-08-15T00:00:00Z', # 'end_date': '2020-12-15T00:00:00Z', # 'enrollment_start': '2019-09-10T13:01:56Z', # 'enrollment_end': '2020-08-29T00:00:00Z', # 'expiration_date': '2021-03-15T00:00:00Z', # 'courseware_url': 'http://edx.odl.local:18000/courses/course-v1:edX+E2E-101+course', # 'courseware_id': 'course-v1:MITx+Digital+Learning+100+Aug_2020', # 'id': 7 # }, # # 'a3': { # 'title': 'SEED Architecture of Complex Systems - Fall 2019', # 'start_date': '2019-08-15T00:00:00Z', # 'end_date': '2019-12-15T00:00:00Z', # 'enrollment_start': None, # 'enrollment_end': '2019-08-29T00:00:00Z', # 'expiration_date': '2020-03-14T00:00:00Z', # 'courseware_url': 'http://edx.odl.local:18000/courses/course-v1:TestX+SysEngxB1+1T2019/course/', # 'courseware_id': 'course-v1:TestX+SysEngxB1+1T2019', # 'id': 2 # }, # # 'a4': { # 'title': 'SEED Models in Engineering - Spring 2020', # 'start_date': '2020-01-15T00:00:00Z', # 'end_date': '2020-05-15T00:00:00Z', # 'enrollment_start': None, 'enrollment_end': '2020-01-29T00:00:00Z', # 'expiration_date': '2020-08-13T00:00:00Z', # 'courseware_url': 'http://edx.odl.local:18000/courses/course-v1:TestX+SysEngxB2+1T2019/course/', # 'courseware_id': 'course-v1:TestX+SysEngxB2+1T2019', # 'id': 3 # }, # # 'a5': { # 'title': 'SEED Model-Based Systems Engineering: Documentation and Analysis - Fall 2020', # 'start_date': '2020-08-15T00:00:00Z', 'end_date': '2020-12-15T00:00:00Z', 'enrollment_start': None, # 'enrollment_end': '2020-08-29T00:00:00Z', 'expiration_date': '2021-03-15T00:00:00Z', # 'courseware_url': 'http://edx.odl.local:18000/courses/course-v1:TestX+SysEngxB3+1T2019/course/', # 'courseware_id': 'course-v1:TestX+SysEngxB3+1T2019', 'id': 4}, # # 'a6': { # 'title': 'SEED Quantitative Methods in Systems Engineering - August 2019', # 'start_date': '2019-08-15T00:00:00Z', # 'end_date': '2019-12-15T00:00:00Z', # 'enrollment_start': '2019-08-15T00:00:00Z', # 'enrollment_end': '2019-08-29T00:00:00Z', # 'expiration_date': '2020-03-14T00:00:00Z', # 'courseware_url': 'http://edx.odl.local:18000/courses/course-v1:TestX+SysEngxB4+1T2019/course/', # 'courseware_id': 'course-v1:TestX+SysEngxB4+1T2019', # 'id': 5 # }, # # 'a2': { # 'title': 'SEED edX Demonstration Course - Spring 2013', # 'start_date': '2013-02-05T00:00:00Z', # 'end_date': None, # 'enrollment_start': None, # 'enrollment_end': None, # 'expiration_date': None, # 'courseware_url': 'http://edx.odl.local:18000/courses/course-v1:edX+DemoX+Demo_Course/course/', # 'courseware_id': 'course-v1:edX+DemoX+Demo_Course', # 'id': 1 # } # }) # # courseruns = OrderedDict(sorted(courses.items(), key=lambda kv: kv[1]['courseware_id'])) # course = json.dumps(courseruns) # print(json.loads(course)) # # # db = \ # # { # # "/data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-17 Disc Wars.mp3": { # # "duration": "00:04:11", # # "artist": "Daft Punk", # # "album": "Tron: Legacy (Cd1)", # # "track": "17", # # "title": "Disc Wars" # # }, # # "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/07 - Godichons.mp3": { # # "duration": "00:05:09", # # "artist": "Keith Kouna", # # "album": "Les ann\u00e9es monsieur", # # "track": "7", # # "title": "Godichons" # # }, # # "/data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-07 Rinzler.mp3": { # # "duration": "00:02:17", # # "artist": "Daft Punk", # # "album": "Tron: Legacy (Cd1)", # # "track": "07", # # "title": "Rinzler" # # }, # # "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/10 - Le Tape.mp3": { # # "duration": "00:06:06", # # "artist": "Keith Kouna", # # "album": "Les ann\u00e9es monsieur", # # "track": "10", # # "title": "Le Tape" # # }, # # "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/09 - L'or.mp3": { # # "duration": "00:05:05", # # "artist": "Keith Kouna", # # "album": "Les ann\u00e9es monsieur", # # "track": "9", # # "title": "L'or" # # } # # } # # # db = OrderedDict(sorted(db.items(), key=lambda t: t[1]['track'])) # # # for key in db: # # # print(key) # # db1 = db # # print(db1) # # # # # print(courses) # # print(OrderedDict(sorted(db.items(), key=lambda t: t[1]['track']))) # # print(db) # Python3 code to demonstrate # Sort nested dictionary by key # using OrderedDict() + sorted() from collections import OrderedDict from operator import getitem import json # initializing dictionary test_dict = {'Akshat' : { 'roll' : 24, 'marks' : 17}, 'Nikhil' : {'roll' : 54, 'marks' : 12}, 'Akash' : { 'roll' : 12, 'marks' : 15}} # printing original dict print("The original dictionary : " + str(test_dict)) # using OrderedDict() + sorted() # Sort nested dictionary by key res = OrderedDict(sorted(test_dict.items(), key = lambda x: getitem(x[1], 'marks'))) print("The sorted dictionary by marks is : " + str(res)) # Output # The sorted dictionary by marks is : OrderedDict([('Akshat', {'marks': 12, 'roll': 54}), ('Akash', {'marks': 15, 'roll': 12}), ('Nikhil', {'marks': 17, 'roll': 24})]) res = json.dumps(res, sort_keys=True) # print result print("The sorted dictionary by marks is : (json)") print(json.loads(res)) # Output # The sorted dictionary by marks is : # {'Akash': {'marks': 15, 'roll': 12}, 'Akshat': {'marks': 12, 'roll': 54}, 'Nikhil': {'marks': 17, 'roll': 24}}