I wrote this code as a test to implement it later in the script I'm working on. The concept is simple, sorting a dictionary in different ways based on the user's choice.
"a" is the default order (the order of data entry in the dictionary). "b" is the default order but reversed. "c" is the default order, but in alphabetical order (A-Z). "d" is the order of "c" but reversed (Z-A).
a = {"pfdee": 100, "pafeeg": 1000, "ddsds": 999, "gfgfgh": 10}
b = {i: a[i] for i in reversed(list(a.keys()))}
c = {i: a[i] for i in sorted(list(a.keys()))}
d = {i: a[i] for i in sorted(list(a.keys()), reverse=True)}
print("a: " + str(a))
print("b: " + str(b))
print("c: " + str(c))
print("d: " + str(d))
Now, I would like to add two further orders, that is related to the quantity of each single object (so "e" should be from the smallest to the largest, "f" should be from the largest to the smallest). I did several tests, but unfortunately they were unsuccessful. I could do it in a normal for loop, but I'd like to keep the dictionary comprehensions if possible. So I ask for advice on how I should proceed, to have a better understanding.
I specify that I am using Python 3.7