2

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.

Mel
  • 5,837
  • 10
  • 37
  • 42
  • Possible duplicate of [How do I sort a list of dictionaries by values of the dictionary in Python?](https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python) – Thom Wiggers Aug 14 '17 at 16:34
  • @ThomWiggers I disagree, because he asks 'why' the sort is according to that field, in addition to the fix. – kabanus Aug 14 '17 at 16:37

2 Answers2

2

The default sort is according to the 'first' key, which you can't control (seems alphabetic to me but I'm unsure - could be a according to the key hash). This doesn't make much sense and surprises many people.

To elaborate, the concept of 'first' and 'second' are undefined with regard to dictionary. Dictionaries are hashes with no guarantee to the order of the keys, by design. It's a technical manner if two different dictionaries with the same keys maintain them in the same order - in particular remember a key need not be a string! So the choice of 'Hacker' first may as well be random as far as you are concerned.

Hence you need to be explicit, which was was enforced in Python 3. One option without another library (itemgetter is the usual favorite):

sorted(student_data,key=lambda x: x['Rank'])

In Python 3, if you trysorted(student_data), an exception will throw an error as dict1 < dict2 is undefined. In Python 2 this isn't enforced leading to the confusion your experiencing.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • Thanks, but i want to know why second key is chosen to sort data? – Gaurav Rathore Aug 14 '17 at 16:44
  • @GauravRathore See edit. Basically, there is no guarantee on the order of dictionary keys, and it just happened to be 'hacker' in your case. Thus the need to be explicit, to define what "dict1 should come before dict2" means. – kabanus Aug 14 '17 at 17:12
1

You must specify the way of how you will sort your list, in your case by the values of the key rank:

from operator import itemgetter

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}]
result = sorted(student_data, key=itemgetter('Rank')) 

print(result)

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