Sorting JSON response with Python
Question: Need to help to figure out how to sort JSON reponse by highest to lowest
number, for example. here is part of JSON reponse below:

    
    
    {
    "queue": "RANKED_SOLO_5x5",
    "name": "Riven's Cutthroats",
    "entries": [
      {
         "leaguePoints": 812,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": false,
         "losses": 277,
         "playerOrTeamName": "CLG Bunso",
         "playerOrTeamId": "19732914",
         "wins": 356
      },
      {
         "leaguePoints": 567,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": false,
         "losses": 56,
         "playerOrTeamName": "SKT Frost",
         "playerOrTeamId": "66401633",
         "wins": 160
      },
      {
         "leaguePoints": 751,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": true,
         "losses": 421,
         "playerOrTeamName": "C9 Hard",
         "playerOrTeamId": "47836799",
         "wins": 494
      },
      {
         "leaguePoints": 587,
         "isFreshBlood": false,
         "isHotStreak": true,
         "division": "I",
         "isInactive": false,
         "isVeteran": false,
         "losses": 157,
         "playerOrTeamName": "ShadowFiendv",
         "playerOrTeamId": "71181475",
         "wins": 265
      },
      {
         "leaguePoints": 1109,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": true,
         "losses": 353,
         "playerOrTeamName": "ApoIlo Price",
         "playerOrTeamId": "7250",
         "wins": 425
      },
    

Now, I already grabbed the necessary info i needed, as below:

    
    
    def getChallengerLadder(region, APIKey):
        URL = "https://" + region + ".api.pvp.net/api/lol/" + region +     "/v2.5/league/challenger?type=RANKED_SOLO_5x5&api_key=" + APIKey
        print (URL)
        response = requests.get(URL)
        return response.json()
    
    
       responseJSON3 = getChallengerLadder(region, APIKey)
        x = 0
        while True:
            print (responseJSON3['entries'][x]['leaguePoints'], responseJSON3['entries'][x]['playerOrTeamName'] )
             x += 1
    

As a result i get a list as follows (again, below is only a small sample):

    
    
    608 Z Y Xydra
    552 Silas Kroeger
    1109 ApoIlo Price
    601 Blem
    587 Boy vs Girl
    701 l am Bjerg
    560 duo to homecomin
    

I want to sort this list from largest to smallest number, but i cant figure
out how to do it. Any help would be appreaciated! IS there a better way to do
it then i already done? I assume you would have to put the response in the
array and sort the array? Or is there a better way to do it?


Answer: Sorted by `leaguePoints`

(I use `json` module only to create full working example)

    
    
    text = '''{
    "queue": "RANKED_SOLO_5x5",
    "name": "Riven's Cutthroats",
    "entries": [
      {
         "leaguePoints": 812,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": false,
         "losses": 277,
         "playerOrTeamName": "CLG Bunso",
         "playerOrTeamId": "19732914",
         "wins": 356
      },
      {
         "leaguePoints": 567,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": false,
         "losses": 56,
         "playerOrTeamName": "SKT Frost",
         "playerOrTeamId": "66401633",
         "wins": 160
      },
      {
         "leaguePoints": 751,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": true,
         "losses": 421,
         "playerOrTeamName": "C9 Hard",
         "playerOrTeamId": "47836799",
         "wins": 494
      },
      {
         "leaguePoints": 587,
         "isFreshBlood": false,
         "isHotStreak": true,
         "division": "I",
         "isInactive": false,
         "isVeteran": false,
         "losses": 157,
         "playerOrTeamName": "ShadowFiendv",
         "playerOrTeamId": "71181475",
         "wins": 265
      },
      {
         "leaguePoints": 1109,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": true,
         "losses": 353,
         "playerOrTeamName": "ApoIlo Price",
         "playerOrTeamId": "7250",
         "wins": 425
      }
    ]
    }'''
    
    import json
    
    # simulate `getChallengerLadder`
    responseJSON3 = json.loads(text)
    
    result = sorted(responseJSON3['entries'], key=lambda x:x['leaguePoints'])
    
    print result
    

