#for dictionary d
sorted(d.items(), key=lambda x: x[1]) #for inceasing order
sorted(d.items(), key=lambda x: x[1], reverse=True) # for decreasing order
#it will return list of key value pair tuples
In [1]: import collections
In [2]: d = {2:3, 1:89, 4:5, 3:0}
In [3]: od = collections.OrderedDict(sorted(d.items()))
In [4]: od
Out[4]: OrderedDict([(1, 89), (2, 3), (3, 0), (4, 5)])
#instead of using python inbuilt function we can it compute directly.
#here iam sorting the values in descending order..
d = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
s=[]
for i in d.items():
s.append(i)
for i in range(0,len(s)):
for j in range(i+1,len(s)):
if s[i][1]<s[j][1]:
s[i],s[j]=s[j],s[i]
print(dict(s))
def sort_dict(dictionary, rev = True):
l = list(dictionary.items())
l.sort(reverse = rev)
a = [item[1] for item in l]
z = ''
for x in a:
z = z + str(x)
return(z)