sorted a dict by value
<p>I read data from a bunch or emails and count frequency of each word. first construct two counters:</p>

<pre><code>counters.stats = collections.defaultdict(dict)
counters.chi = collections.counter()
</code></pre>

<p>The key of stats is word. For each word, I construct a dict, whose key is the name of the email and value is the frequency of that word in this email.</p>

<p>The key of chi is the same words as those in stats. I want to sort the key in 'stats' by the keys in 'chi.' The problem is fixed by:</p>

<pre><code>def print_stats(counters):
    sorted_key = sorted(counters.stats, key = counters.chi.get)
    result = collections.OrderedDict(k, counters.stats[k] for key in sorted_key)
        for form, cat_to_stats in result.items():
</code></pre>
