stouputils.collections.sorting module#
- sort_dict_keys( ) dict[T, Any][source]#
Sort dictionary keys using a given order list (reverse optional)
- Parameters:
dictionary – The dictionary to sort
order – The order list
reverse – Whether to sort in reverse order (given to sorted function which behaves differently than order.reverse())
- Returns:
The sorted dictionary
>>> sort_dict_keys({'b': 2, 'a': 1, 'c': 3}, order=["a", "b", "c"]) {'a': 1, 'b': 2, 'c': 3}
>>> sort_dict_keys({'b': 2, 'a': 1, 'c': 3}, order=["a", "b", "c"], reverse=True) {'c': 3, 'b': 2, 'a': 1}
>>> sort_dict_keys({'b': 2, 'a': 1, 'c': 3, 'd': 4}, order=["c", "b"]) {'c': 3, 'b': 2, 'a': 1, 'd': 4}