Using list of dictionary

Viewed 78

I have dictionary data like this

student_data = [{'id':1, 'Hacker' : 'DOSHI', 'Rank' : 43},
 {'id':2, 'Hacker' : 'JOSHI', 'Rank' : 45},
 {'id':3, 'Hacker' : 'MOSHI', 'Rank' : 41},
 {'id':4, 'Hacker' : 'LOSHI', 'Rank' : 98},
 {'id':5, 'Hacker' : 'AOSHI', 'Rank' : 14}]

when I using sorted to sort list of dictionary, my code is

print sorted(student_data), 

Output is:

[{'Hacker': 'AOSHI', 'id': 5, 'Rank': 14},
 {'Hacker': 'DOSHI', 'id': 1, 'Rank': 43},
 {'Hacker': 'JOSHI', 'id': 2, 'Rank': 45},
 {'Hacker': 'LOSHI', 'id': 4, 'Rank': 98},
 {'Hacker': 'MOSHI', 'id': 3, 'Rank': 41}]

List of dictionary sorted on the basis of key "Hacker", I want to sort data on the basis of "Rank".
Can any one explain why list of dictionary use key "Hacker" to sort? Other keys are "id" and "Rank", but sorted on the basis of "Hacker" which is not the first key but chosen to sort the elements.

2 Answers
Related