-2

I have a code for the Euros that shows the tables and fixtures etc. With the tables, I have them stored in a json file in a set order that doesn't change, like this:

    "Table": {
      "Italy": {
        "Played": 0,
        "Won": 0,
        "Draw": 0,
        "Lost": 0,
        "PF": 0,
        "PA": 0,
        "PD": 0,
        "Points": 0
      },
      "Switzerland": {
        "Played": 0,
        "Won": 0,
        "Draw": 0,
        "Lost": 0,
        "PF": 0,
        "PA": 0,
        "PD": 0,
        "Points": 0
      },
      "Turkey": {
        "Played": 0,
        "Won": 0,
        "Draw": 0,
        "Lost": 0,
        "PF": 0,
        "PA": 0,
        "PD": 0,
        "Points": 0
      },
      "Wales": {
        "Played": 0,
        "Won": 0,
        "Draw": 0,
        "Lost": 0,
        "PF": 0,
        "PA": 0,
        "PD": 0,
        "Points": 0
      }
    }

I want to be able to sort the teams by the amount of points they have, or the points difference if the points are the same. Ive looked at Python: sorting dictionary of dictionaries and other related questions and still not been able to figure out how it is possible, any help please?

Legacy Coding
  • 544
  • 2
  • 6
  • 30
  • Does this answer your question? [Python: sorting dictionary of dictionaries](https://stackoverflow.com/questions/16412563/python-sorting-dictionary-of-dictionaries) – Hotkuk Jun 11 '21 at 13:02
  • @Hotkuk i already have the link to that question in my question! – Legacy Coding Jun 11 '21 at 13:03
  • 1
    Yes, because your question is already explained in the link, don't know why it created auto-comment, but I just flagged your question as a duplicate – Hotkuk Jun 11 '21 at 13:07
  • if you want to sort it by some values than maybe keep it in database or in pandas dataframe – furas Jun 11 '21 at 13:12

1 Answers1

0

As said in another thread, you can't sort a dictionary because it is how it works, the workaround here would be to create list of dicts like this, but still, it's pain to work with it and you lose advantages of dictionary

table = [
{"Italy": {...}},
{"Switzerland": {...}},
...
{"Wales": {...}},
]

And another workaround will be to create OrderedDict, but it's already explained in the link in your question

Hotkuk
  • 114
  • 9