nodes: 0, module; 1, function_definition; 2, function_name:sort_keys; 3, parameters; 4, block; 5, identifier:self; 6, identifier:keys; 7, default_parameter; 8, expression_statement; 9, comment:# to do: check that all reverse index values are unambiguous; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, if_statement; 14, identifier:order; 15, attribute; 16, comment:"""Sort keys.
  
          Keys are sorted based on the value they are indexing.
  
          :param keys: Keys to be sorted
          :type keys: list(str)
          :param order: Order criteri (asending or descending)
          :type order: int
          :return: Sorted keys
          :rtype: list(str)
          :raise ValueError: If invalid order value is passed
  
          """; 17, assignment; 18, assignment; 19, assignment; 20, comparison_operator:order == QuerySet.ASCENDING; 21, block; 22, elif_clause; 23, else_clause; 24, identifier:QuerySet; 25, identifier:ASCENDING; 26, identifier:missing_keys; 27, list_comprehension; 28, identifier:keys_and_values; 29, list_comprehension; 30, identifier:sorted_keys; 31, list_comprehension; 32, identifier:order; 33, attribute; 34, return_statement; 35, comparison_operator:order == QuerySet.DESCENDING; 36, block; 37, block; 38, identifier:key; 39, for_in_clause; 40, if_clause; 41, tuple; 42, for_in_clause; 43, if_clause; 44, subscript; 45, for_in_clause; 46, identifier:QuerySet; 47, identifier:ASCENDING; 48, binary_operator:missing_keys + sorted_keys; 49, identifier:order; 50, attribute; 51, return_statement; 52, raise_statement; 53, identifier:key; 54, identifier:keys; 55, not_operator; 56, identifier:key; 57, subscript; 58, identifier:key; 59, identifier:keys; 60, comparison_operator:key not in missing_keys; 61, identifier:kv; 62, integer:0; 63, identifier:kv; 64, call; 65, identifier:missing_keys; 66, identifier:sorted_keys; 67, identifier:QuerySet; 68, identifier:DESCENDING; 69, binary_operator:sorted_keys + missing_keys; 70, call; 71, call; 72, subscript; 73, integer:0; 74, identifier:key; 75, identifier:missing_keys; 76, identifier:sorted; 77, argument_list; 78, identifier:sorted_keys; 79, identifier:missing_keys; 80, identifier:ValueError; 81, argument_list; 82, identifier:len; 83, argument_list; 84, attribute; 85, identifier:key; 86, identifier:keys_and_values; 87, keyword_argument; 88, keyword_argument; 89, call; 90, subscript; 91, identifier:self; 92, identifier:_reverse_index; 93, identifier:key; 94, lambda; 95, identifier:reverse; 96, conditional_expression:True if order == QuerySet.DESCENDING else False; 97, attribute; 98, argument_list; 99, attribute; 100, identifier:key; 101, lambda_parameters; 102, subscript; 103, True; 104, comparison_operator:order == QuerySet.DESCENDING; 105, False; 106, string; 107, identifier:format; 108, identifier:order; 109, identifier:self; 110, identifier:_reverse_index; 111, identifier:x; 112, identifier:x; 113, integer:1; 114, identifier:order; 115, attribute; 116, string_content:Unexpected order value: {:d}; 117, identifier:QuerySet; 118, identifier:DESCENDING
code: def sort_keys(self, keys, order=QuerySet.ASCENDING):
          """Sort keys.
  
          Keys are sorted based on the value they are indexing.
  
          :param keys: Keys to be sorted
          :type keys: list(str)
          :param order: Order criteri (asending or descending)
          :type order: int
          :return: Sorted keys
          :rtype: list(str)
          :raise ValueError: If invalid order value is passed
  
          """
          # to do: check that all reverse index values are unambiguous
          missing_keys = [
              key
              for key in keys
              if not len(self._reverse_index[key])
          ]
          keys_and_values = [
              (key, self._reverse_index[key][0])
              for key in keys
              if key not in missing_keys
          ]
          sorted_keys = [
              kv[0]
              for kv in sorted(
                  keys_and_values,
                  key=lambda x: x[1],
                  reverse=True if order == QuerySet.DESCENDING else False)
          ]
          if order == QuerySet.ASCENDING:
              return missing_keys + sorted_keys
          elif order == QuerySet.DESCENDING:
              return sorted_keys + missing_keys
          else:
              raise ValueError('Unexpected order value: {:d}'.format(order))