I have a dictionary which has the following content :
dict = {'P1' :361 , 'P2' : 361, 'P3' : 391, 'P6' : 361, 'P4':361, 'P5' :361}
which needs to be sorted on basis of value in descending order. For any given item if the value is same to the item it is compared, it is to be sorted on the ascending order of key. Hence, the resultant value of the dict should be this.
{'P3' : 391 , 'P1' : 361, 'P2': 361, 'P4': 361, 'P5': 361,'P6' : 361}
I have achieved the first part using this :
sorted_dict = OrderedDict(sorted(dict.items(), key=lambda t: t[1],reverse=True))
However, I am uncertain how to go beyond that. I am assuming the comparator function typically can be something like this
def comparator(item1,item2):
if item1.value() == item2.value():
if item1.key()<item2.key():
return -1
if item1.key()>item2.key():
return 1
if item1.key() == item2.key():
return 0
if item1.value() < item2.value():
return -1
if item1.value() > item2.value():
return 1
I am unsure of how to implement this. Pardon my naiveté, I am a beginner in Python and have little knowledge of the programming constructs in the language.