Home > OS >  How do I sort this data from a json string
How do I sort this data from a json string

Time:04-16

{
      "version": 51,
      "players": [
        {
          "awards": [],
          "born": {
            "year": 1993,
            "loc": "British Columbia, Canada"
          },
          "college": "",
          "contract": {
            "amount": 750,
            "exp": 2023
          },
          "draft": {
            "round": 4,
            "pick": 2,
            "tid": 19,
            "year": 2011,
            "originalTid": 19,
            "pot": 56,
            "ovr": 32,
            "skills": []

this is part of my json file I want to sort OVR on many of the players **

i.sorted(key=ovr, reverse=True)

is what I tried and nothing worked any suggestions

CodePudding user response:

By usong the sorted function you can use a function to specify what you want to sort by.

Since your question is unclear I'll assume that you have a list of dictionaries like in the example you posted in your question. We'll call that list: data["players"] since that's where it seems to be in your JSON.

Then you can sort that list via:

sorted_players = sorted(data["players"], key=lambda x: x["draft"]["ovr"])

Let me know if that works for you.

  • Related