0

I wanted to sort my dictionary in reverse order, order by nested dict key 0:

mydict = {
          'key1': {0: 3, 1: ["doc1.txt", "doc2.txt"], 2: ["text1", "text2"]},
          'key2': {0: 8, 1: ["doc6.txt", "doc7.txt"], 2: ["text3", "text4"]},
          'key3': {0: 1, 1: ["doc8.txt", "doc9.txt"], 2: ["text7", "text8"]},
}

to have this order:

'key3': {0: 1, 1: ['doc8.txt', 'doc9.txt'], 2: ['text7', 'text8']}
'key1': {0: 3, 1: ['doc1.txt', 'doc2.txt'], 2: ['text1', 'text2']}
'key2': {0: 8, 1: ['doc6.txt', 'doc7.txt'], 2: ['text3', 'text4']}

I've tried:

import operator

sorted_dict = sorted(mydict.items(), key=operator.itemgetter(0), reverse=True)

But no success.

jturi
  • 1,615
  • 15
  • 11
  • Possible duplicate of [Sort a Python dictionary by value](https://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – Ilja Everilä Jul 21 '17 at 13:43
  • 1
    [`sorted()`](https://docs.python.org/3/library/functions.html#sorted) creates a new sorted list – in your case a list of tuples representing the dictionary items. Dicts are still unordered in Python. [The new order preserving representation in 3.6 and newer](https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-compactdict) is considered an implementation detail and should not be relied upon. If you need an ordered dict, use [collections.OrderedDict](https://docs.python.org/3/library/collections.html#collections.OrderedDict). – Ilja Everilä Jul 21 '17 at 13:49
  • This isn't clear does the OP want to sort the items in his dictionary, or does he want to sort the items in is dictionary *and keep them in a dictionary structure*. If the former then @IljaEverilä's advice is the most pertinent, if the latter, then my answer is. – Tom Wyllie Jul 21 '17 at 13:52

3 Answers3

0

You were close, but I'd suggest using a lambda function, which pulls the relevant value out of the dictionary in index one for each item;

sorted_dict = sorted(mydict.items(), key=lambda x: x[1][0])

Printing the output into a format where we can easily observe the order outputs;

for item in sorted_dict:
    print(item)

Which outputs;

('key3', {0: 1, 1: ['doc8.txt', 'doc9.txt'], 2: ['text7', 'text8']})
('key1', {0: 3, 1: ['doc1.txt', 'doc2.txt'], 2: ['text1', 'text2']})
('key2', {0: 8, 1: ['doc6.txt', 'doc7.txt'], 2: ['text3', 'text4']})
Tom Wyllie
  • 2,020
  • 13
  • 16
  • Thank you for the quick answer, this sorted method kept the order of the dictionary and the dict type. It was perfectly working with my Jinja2 templates cycling through the results. Cheers! – jturi Jul 21 '17 at 16:41
  • @JozsefTuri No problem :) – Tom Wyllie Jul 21 '17 at 16:42
0

Instead of :

sorted_dict = sorted(mydict.items(), key=operator.itemgetter(0), reverse=True)

you should use:

sorted_dict = sorted(mydict.items(), key=operator.itemgetter(1))

It will sort the dictionary to your satisfaction!

unkn0wn1
  • 11
  • 1
0

Dictionaries are unordered in Python, and unless you use collections.OrderedDict, you will have to convert your structure to tuples and apply the sorted function

my_dict = mydict.items()

final = sorted([(a, b.items()) for a, b in my_dict], key=lambda x:x[1][0][1])

Output:

[('key3', [(0, 1), (1, ['doc8.txt', 'doc9.txt']), (2, ['text7', 'text8'])]), 
 ('key1', [(0, 3), (1, ['doc1.txt', 'doc2.txt']), (2, ['text1', 'text2'])]), 
 ('key2', [(0, 8), (1, ['doc6.txt', 'doc7.txt']), (2, ['text3', 'text4'])])]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102