code: def sort_dict(d, key=None, reverse=False):
      kv_items = [kv for kv in d.items()]
      if key is None:
          kv_items.sort(key=lambda t: t[1], reverse=reverse)
      else:
          kv_items.sort(key=key, reverse=reverse)
      return collections.OrderedDict(kv_items)