nodes: 0, module; 1, function_definition; 2, function_name:sort_dict; 3, parameters; 4, block; 5, identifier:dict_; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, if_statement; 12, expression_statement; 13, expression_statement; 14, return_statement; 15, identifier:part; 16, string; 17, identifier:key; 18, None; 19, identifier:reverse; 20, False; 21, comment:"""
      sorts a dictionary by its values or its keys
  
      Args:
          dict_ (dict_):  a dictionary
          part (str): specifies to sort by keys or values
          key (Optional[func]): a function that takes specified part
              and returns a sortable value
          reverse (bool): (Defaults to False) - True for descinding order. False
              for ascending order.
  
      Returns:
          OrderedDict: sorted dictionary
  
      CommandLine:
          python -m utool.util_dict sort_dict
  
      Example:
          >>> # ENABLE_DOCTEST
          >>> from utool.util_dict import *  # NOQA
          >>> import utool as ut
          >>> dict_ = {'a': 3, 'c': 2, 'b': 1}
          >>> results = []
          >>> results.append(sort_dict(dict_, 'keys'))
          >>> results.append(sort_dict(dict_, 'vals'))
          >>> results.append(sort_dict(dict_, 'vals', lambda x: -x))
          >>> result = ut.repr4(results)
          >>> print(result)
          [
              {'a': 3, 'b': 1, 'c': 2},
              {'b': 1, 'c': 2, 'a': 3},
              {'a': 3, 'c': 2, 'b': 1},
          ]
      """; 22, comparison_operator:part == 'keys'; 23, block; 24, elif_clause; 25, else_clause; 26, comparison_operator:key is None; 27, block; 28, else_clause; 29, assignment; 30, assignment; 31, identifier:sorted_dict; 32, string_content:keys; 33, identifier:part; 34, string; 35, expression_statement; 36, comparison_operator:part in {'vals', 'values'}; 37, block; 38, block; 39, identifier:key; 40, None; 41, expression_statement; 42, block; 43, identifier:sorted_items; 44, call; 45, identifier:sorted_dict; 46, call; 47, string_content:keys; 48, assignment; 49, identifier:part; 50, set; 51, expression_statement; 52, raise_statement; 53, assignment; 54, function_definition; 55, identifier:sorted; 56, argument_list; 57, identifier:OrderedDict; 58, argument_list; 59, identifier:index; 60, integer:0; 61, string; 62, string; 63, assignment; 64, call; 65, identifier:_key; 66, call; 67, function_name:_key; 68, parameters; 69, block; 70, call; 71, keyword_argument; 72, keyword_argument; 73, identifier:sorted_items; 74, string_content:vals; 75, string_content:values; 76, identifier:index; 77, integer:1; 78, identifier:ValueError; 79, argument_list; 80, attribute; 81, argument_list; 82, identifier:item; 83, return_statement; 84, attribute; 85, argument_list; 86, identifier:key; 87, identifier:_key; 88, identifier:reverse; 89, identifier:reverse; 90, binary_operator:'Unknown method part=%r' % (part,); 91, identifier:op; 92, identifier:itemgetter; 93, identifier:index; 94, call; 95, identifier:six; 96, identifier:iteritems; 97, identifier:dict_; 98, string; 99, tuple; 100, identifier:key; 101, argument_list; 102, string_content:Unknown method part=%r; 103, identifier:part; 104, subscript; 105, identifier:item; 106, identifier:index
code: def sort_dict(dict_, part='keys', key=None, reverse=False):
      """
      sorts a dictionary by its values or its keys
  
      Args:
          dict_ (dict_):  a dictionary
          part (str): specifies to sort by keys or values
          key (Optional[func]): a function that takes specified part
              and returns a sortable value
          reverse (bool): (Defaults to False) - True for descinding order. False
              for ascending order.
  
      Returns:
          OrderedDict: sorted dictionary
  
      CommandLine:
          python -m utool.util_dict sort_dict
  
      Example:
          >>> # ENABLE_DOCTEST
          >>> from utool.util_dict import *  # NOQA
          >>> import utool as ut
          >>> dict_ = {'a': 3, 'c': 2, 'b': 1}
          >>> results = []
          >>> results.append(sort_dict(dict_, 'keys'))
          >>> results.append(sort_dict(dict_, 'vals'))
          >>> results.append(sort_dict(dict_, 'vals', lambda x: -x))
          >>> result = ut.repr4(results)
          >>> print(result)
          [
              {'a': 3, 'b': 1, 'c': 2},
              {'b': 1, 'c': 2, 'a': 3},
              {'a': 3, 'c': 2, 'b': 1},
          ]
      """
      if part == 'keys':
          index = 0
      elif part in {'vals', 'values'}:
          index = 1
      else:
          raise ValueError('Unknown method part=%r' % (part,))
      if key is None:
          _key = op.itemgetter(index)
      else:
          def _key(item):
              return key(item[index])
      sorted_items = sorted(six.iteritems(dict_), key=_key, reverse=reverse)
      sorted_dict = OrderedDict(sorted_items)
      return sorted_dict