stouputils.collections.sort_dict_keys module#

sort_dict_keys(
dictionary: dict[T, Any],
order: list[T],
reverse: bool = False,
) dict[T, Any][source]#

Sort dictionary keys using a given order list (reverse optional)

Parameters:
  • dictionary (dict[T, Any]) – The dictionary to sort

  • order (list[T]) – The order list

  • reverse (bool) – Whether to sort in reverse order (given to sorted function which behaves differently than order.reverse())

Returns:

The sorted dictionary

Return type:

dict[T, Any]

Examples

>>> 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}