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>You can use:</p>

<pre><code>sorted(d, key=lambda key:(d[key]['a'], d[key]['b'], d[key]['c']))
</code></pre>

<p>And here is a general solution in case you have an arbitrary number of elements in the inner dictionaries:</p>

<pre><code>sorted(d, key=lambda key:[value for value in sorted(d[key].items())])
</code></pre>

python sorting dictionary ranking