Variations on this come up all the time. Note that arrays can be
sorted.
So, create an array of tuples containing the key and the value.
Then you can sort the array anyway you please.
In the view:
# create the array of tuples
ordered_dict = [(key, val) for key,val in my_original_dict.items()]
#
I have a dict that looks similar to this:
{'com': 'communication', 'tel': 'telephone', 'cel': 'cellphone',
'fax': 'fax machine',}
And I want to sort it by value, and render it in a template. I was
able to do it, but in a very hacked way. Can anyone show me a better
way to do it than my solution.
2 matches
Mail list logo