Sorting list with dictionaries values(Maximum to Minimum)
<p>I have an array with loads of dictionaries in it. However I want to sort dictionaries in a way where I have maximum value to a specific key in a dictionary.
For example I have a list that looks like this</p>

<pre><code>[
    {
        "num_gurus": 40,
        "id": 119749,
        "code": null,
        "name": "ART 198P",
        "short_name": "ART 198P",
        "title": "Directed Group Study",
        "department_long": null,
        "full_name": "Directed Group Study",
        "department_short": "ART"
    },
    {
        "num_gurus": 3,
        "id": 119825,
        "code": null,
        "name": "ASAMST 198P",
        "short_name": "ASAMST 198P",
        "title": "Supervised Group Study",
        "department_long": null,
        "full_name": "Supervised Group Study",
        "department_short": "ASAMST"
    },
    {
        "num_gurus": 200,
        "id": 119904,
        "code": null,
        "name": "AST 636",
        "short_name": "AST 636",
        "title": "Baudelaire: Art Poetry Modernity",
        "department_long": null,
        "full_name": "Baudelaire: Art Poetry Modernity",
        "department_short": "AST"
    }
]
</code></pre>

<p>I want my output to sort my dictionaries where the value of a key attribute 'num_gurus' is maximum to minimum. Expected output would be.</p>

<pre><code>[
    {
        "num_gurus": 200,
        "id": 119904,
        "code": null,
        "name": "AST 636",
        "short_name": "AST 636",
        "title": "Baudelaire: Art Poetry Modernity",
        "department_long": null,
        "full_name": "Baudelaire: Art Poetry Modernity",
        "department_short": "AST"
    }
    {
        "num_gurus": 40,
        "id": 119749,
        "code": null,
        "name": "ART 198P",
        "short_name": "ART 198P",
        "title": "Directed Group Study",
        "department_long": null,
        "full_name": "Directed Group Study",
        "department_short": "ART"
    },
    {
        "num_gurus": 3,
        "id": 119825,
        "code": null,
        "name": "ASAMST 198P",
        "short_name": "ASAMST 198P",
        "title": "Supervised Group Study",
        "department_long": null,
        "full_name": "Supervised Group Study",
        "department_short": "ASAMST"
    }

]
</code></pre>

<p>I have tried this so far</p>

<pre><code>    for items in load_as_json:
            for key, val in sorted(items['num_gurus'].iteritems(), key=lambda (k,v): (v,k), reverse=True):
                print key,val

This throws me error and doesn't do what I actually want to.
This is the error I got.
  File "utils.py", line 61, in GetPopularCoursesBasedOnGurus
    for key, val in sorted(str(items['num_gurus']).iteritems(), key=lambda (k,v): (v,k)):
AttributeError: 'str' object has no attribute 'iteritems'
</code></pre>

<p>For <strong><em>storing the sorted list as new list</em></strong>, you can do it using <a href="https://docs.python.org/2/library/functions.html#sorted" rel="nofollow"><code>sorted()</code></a> as:</p>

<pre><code>sorted(my_list, key=lambda x: x['num_gurus'], reverse=True)
# returns sorted list
</code></pre>

<p>where <code>my_list</code> is your <code>list</code> of <code>dict</code> objects.</p>

<p>Else, if you want to <strong><em>sort the content of original list</em></strong>, i.e <code>my_list</code>, then use <code>list.sort()</code> as:</p>

<pre><code>my_list.sort(key=lambda x: x["num_gurus"], reverse=True)
# sorts the original list
</code></pre>

<p>Check document on: <a href="https://wiki.python.org/moin/HowTo/Sorting" rel="nofollow">How to do Sorting in list</a></p>

python arrays sorting dictionary