Python Forum

Full Version: How can I sort my dict ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have it
{1: ['3', ['third task', True]], 2: ['1', ['first task', True]], 3: ['2', ['second task', True]]}
I need to get sort dict like that

{1: ['1', ['first task', True]], 2: ['2', ['second task', True]], 3: ['3', ['third task', True]]}
Since Python 3.7, dictionaries are guaranteed to be ordered by insertion order. Before that, you would need to use collections.OrderedDict. Once you have a dictionary ordered by insertion order, you sort it by getting the keys, sorting the keys, and then reinserting the key/value pairs in that order.