0

I have been working on a python script, but have come across a problem with sorting my dictionary.

I need to sort it by a value’s value from greatest to least. Here is my dictionary setup:

{“Value1”:{“OtherValue”:0,”ValueINeed”:50},”Value2”:{“OtherValue”:1,”ValueINeed”:100}}

the expected result would be:

{“Value2”:{“OtherValue”:1,”ValueINeed”:100},”Value1”:{“OtherValue”:0,”ValueINeed”:50}}

How can I achieve this?

JackTYM
  • 13
  • 4
  • 1
    Dictionarys and set are unordered entities - by design they are insert order sorted since a few versions. Why ever would you need to sort them? For outputting? – Patrick Artner Aug 21 '21 at 19:06
  • As dictionaries contain key-value pairs I do not understand the need to "sort" a dictionary, it is by definition unordered. I assume you need to convert this into a list before trying to sort it. – Joshua Smart Aug 21 '21 at 19:06
  • Wow, I'm really bummed someone closed this one on you, OP. Here's your answer: `d={'Value1': {'OtherValue': 0, 'ValueINeed': 50},'Value2': {'OtherValue': 1, 'ValueINeed': 100}} ;dict(sorted(d.items(), key=lambda x: x[1]["OtherValue"], reverse=True))` I was hoping to give you more detail, but trigger happy high-rep users shot your thread dead. Good luck, and welcome to Stack Overflow. Hopefully this doesn't discourage you from posting again. – user1717828 Aug 21 '21 at 19:11
  • https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value Have you tried this, should solve your problem! –  Aug 21 '21 at 19:15

0 Answers0