I have a dictionary with ints for keys, and strings for values. I need to to sort by the strings, so that when I go dict.values() I get the sorted list.
The strings are values like this: 45_12_something_23
I need to sort numbers as numbers and strings as strings. A given row is guaranteed to be either a string or a number (not a mixture).
Whats a good way to do this in python? Performance isnt an issue.
Convert your dictionary to a list of (key,value) pairs. Sort them however you’d like. (Or is that your question – how to do that?) Then insert the sorted (key,value) pairs into a
collections.OrderedDict, which will remember the insertion order and use the same order when iterating.Note that you can’t modify the
dictyou already have, this will make a newdictwith the properties you want.