How to sort list of dictionaries by value
<pre><code>dictionary[pattern_key] = {"key": index_key, "document": index_source, "startPos":index_start, "endPos": index_end}
</code></pre>

<p>This is an extract of my list of dictionaries</p>

<pre><code>{
'AGACAATCTC': {'startPos': '174', 'document': 'source-document01012.txt', 'endPos': '183', 'key': 'AGACAATCTC'}, 
'GGTCAGACAA': {'startPos': '18', 'document': 'source-document01012.txt', 'endPos': '27', 'key': 'GGTCAGACAA'}, 
'TAGATGAAGT': {'startPos': '102', 'document': 'source-document01012.txt', 'endPos': '111', 'key': 'TAGATGAAGT'}
}
</code></pre>

<p>How can i sort that by <strong>document</strong> and then by <strong>startPos</strong> ?</p>

<p>i tried something like this but does not work</p>

<p><code>sorted_dict = sorted(dictionary, key=itemgetter(pattern_key[document]))</code></p>

<p>script.py</p>

<pre><code>#!/usr/bin/env python
import sys

dictionary = {};

for pattern in sys.stdin:

    if "," in pattern:
        pattern_key, pattern_source, pattern_start, pattern_end = pattern.strip().split(",")
        index_file =  open('index.txt', 'r')

        for line in index_file:
            if "," in line:
                index_key, index_source, index_start, index_end = line.strip().split(",")
                if pattern_key == index_key:
                    dictionary[pattern_key] = {"document": index_source, "startPos":index_start, "endPos": index_end}

sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))

for k, v in dictionary.items():
    print (k, '--&gt;', v)
</code></pre>