nodes: 0, module; 1, function_definition; 2, function_name:add_query_parameters_to_url; 3, parameters; 4, block; 5, identifier:url; 6, identifier:query_parameters; 7, expression_statement; 8, comment:# Parse the given URL into parts.; 9, expression_statement; 10, comment:# Parse existing parameters and add new parameters.; 11, expression_statement; 12, expression_statement; 13, comment:# Sort parameters to ensure consistent order.; 14, expression_statement; 15, for_statement; 16, comment:# Encode the new parameters and return the updated URL.; 17, expression_statement; 18, return_statement; 19, comment:"""
      Merge a dictionary of query parameters into the given URL.
      Ensures all parameters are sorted in dictionary order when returning the URL.
      """; 20, assignment; 21, assignment; 22, call; 23, assignment; 24, identifier:k; 25, call; 26, block; 27, assignment; 28, call; 29, identifier:url_parts; 30, call; 31, identifier:qs_args; 32, call; 33, attribute; 34, argument_list; 35, identifier:sorted_qs_args; 36, call; 37, identifier:sorted; 38, argument_list; 39, expression_statement; 40, identifier:new_qs; 41, call; 42, attribute; 43, argument_list; 44, attribute; 45, argument_list; 46, attribute; 47, argument_list; 48, identifier:qs_args; 49, identifier:update; 50, identifier:query_parameters; 51, identifier:OrderedDict; 52, argument_list; 53, call; 54, assignment; 55, attribute; 56, argument_list; 57, attribute; 58, identifier:urlunparse; 59, binary_operator:list(url_parts[0:4]) + [new_qs] + list(url_parts[5:]); 60, attribute; 61, identifier:urlparse; 62, identifier:url; 63, attribute; 64, identifier:parse_qs; 65, subscript; 66, attribute; 67, argument_list; 68, subscript; 69, subscript; 70, attribute; 71, identifier:urlencode; 72, identifier:sorted_qs_args; 73, True; 74, identifier:urllib; 75, identifier:parse; 76, binary_operator:list(url_parts[0:4]) + [new_qs]; 77, call; 78, identifier:urllib; 79, identifier:parse; 80, identifier:urllib; 81, identifier:parse; 82, identifier:url_parts; 83, integer:4; 84, identifier:qs_args; 85, identifier:keys; 86, identifier:sorted_qs_args; 87, identifier:k; 88, identifier:qs_args; 89, identifier:k; 90, identifier:urllib; 91, identifier:parse; 92, call; 93, list; 94, identifier:list; 95, argument_list; 96, identifier:list; 97, argument_list; 98, identifier:new_qs; 99, subscript; 100, subscript; 101, identifier:url_parts; 102, slice; 103, identifier:url_parts; 104, slice; 105, integer:5; 106, integer:0; 107, integer:4
code: def add_query_parameters_to_url(url, query_parameters):
      """
      Merge a dictionary of query parameters into the given URL.
      Ensures all parameters are sorted in dictionary order when returning the URL.
      """
      # Parse the given URL into parts.
      url_parts = urllib.parse.urlparse(url)
  
      # Parse existing parameters and add new parameters.
      qs_args = urllib.parse.parse_qs(url_parts[4])
      qs_args.update(query_parameters)
  
      # Sort parameters to ensure consistent order.
      sorted_qs_args = OrderedDict()
      for k in sorted(qs_args.keys()):
          sorted_qs_args[k] = qs_args[k]
  
      # Encode the new parameters and return the updated URL.
      new_qs = urllib.parse.urlencode(sorted_qs_args, True)
      return urllib.parse.urlunparse(list(url_parts[0:4]) + [new_qs] + list(url_parts[5:]))