Sort a dictionary of arrays in python with a tuple as the key <p>I a dictionary with a string tuple as the key and an array as the value:</p> <pre><code>some_dict = {} some_dict[(A,A)] = [1234, 123] some_dict[(A,B)] = [1235, 13] some_dict[(A,C)] = [12, 12] some_dict[(B,B)] = [12621, 1] ... </code></pre> <p>I am writing this to a csv file:</p> <pre><code>for k,v in some_dict.iteritems(): outf.write(k[0]+","+k[1]+","+str(v[0])+","str(v[1])+"\n") </code></pre> <p>output:</p> <pre><code>A,A,1234,123 A,B,1235,13 A,C,12,12 B,B,12621,1 </code></pre> <p>I'd like to sort it based on the first number "column" before writing to a file (or after and just rewrite?) so the output should be like:</p> <pre><code>B,B,12621,1 A,B,1235,13 A,A,1234,123 A,C,12,12 ^ Sorted on this 'column' </code></pre> <p>How do I do this? The file is very large, so doing it before writing would be better I think.</p>