code: def sort_dict(d, key=None, reverse=False):
      
      kv_items = [kv for kv in d.items()]
  
      # Sort kv_items according to key.
      if key is None:
          kv_items.sort(key=lambda t: t[1], reverse=reverse)
      else:
          kv_items.sort(key=key, reverse=reverse)
  
      # Build ordered dict.
      return collections.OrderedDict(kv_items)
docs: Sorts a dict by value.
  
      Args:
          d: Input dictionary
          key: Function which takes an tuple (key, object) and returns a value to
              compare and sort by. By default, the function compares the values
              of the dict i.e. key = lambda t : t[1]
          reverse: Allows to reverse sort order.
  
      Returns:
          OrderedDict object whose keys are ordered according to their value.