Skip to content
Advertisement

Sort based Dictionary of Dictionary of Dictionary Values?

I have a dictionary like this

JavaScript

I am trying to sort dictionary based on KEY OF LAST VALUES(LIST).

FOR EXAMPLE

I am looking for get dictionary value like ‘C’ IN first and ‘D’ in second

I’m trying to get correct order. Here is code:

JavaScript

Unfortunately didn’t get correct result

This is my expected output

JavaScript

I am using python 3.5 . I am trying to get order like this

JavaScript

Advertisement

Answer

The following code will sort any dictionary by its key and recursively sort any dictionary value that is also a dictionary by its key and makes no assumption as to the content of the dictionary being sorted. It uses an OrderedDict but if you can be sure it will always run on Python 3.6 or greater, a simple change can be made to use a dict.

JavaScript

See demo

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement