Sorting the keys of a dictionary based on different values
<p>I have a dictionary:</p>

<pre><code>d = {"A":{"a":1, "b":2, "c":3}, "B":{"a":5, "b":6, "c":7}, "C":{"a":4, "b":6, "c":7}}
</code></pre>

<p>I want to sort the keys "A", "B" and "C" in a list, first on the basis of numerical values of "a", then if some tie occurs on the basis of numerical values of "b" and so on. </p>

<p>How can I do it?</p>

<p>Make a list of your dictionary like so:</p>

<pre><code>my_list = [(key, value) for item in d.items()]
</code></pre>

<p>Then sort the list using whatever criteria you have in mind:</p>

<pre><code>def sort_function(a, b):
   # whatever complicated sort function you like
   return True if a &gt; b else False

my_list.sort(sort_function)
</code></pre>

python sorting dictionary ranking