-1

I have a dictionary like the following:

{2234:A0,432:A16,3336:A0,11:A0,3:A16}

and I want to sort the dictionary based on its keys like the following:

{3:A16,11:A0,432:A16,2234:A0,3336:A0}

Here is what I have tried but it doesn't sort the dictionary.

sorted(a_dict.items())
print(a_dict)
ttina
  • 87
  • 1
  • 9

1 Answers1

0

Sort it by key first, and then integer part of the value next , example:

dict(sorted(sorted(d.items()), key= lambda x: int(x[1][1:]), reverse=True))

Gives

{3: 'A16', 432: 'A16', 11: 'A0', 2234: 'A0', 3336: 'A0'}
Kris
  • 8,680
  • 4
  • 39
  • 67