nodes: 0, module; 1, function_definition; 2, function_name:sortlevel; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, import_from_statement; 11, if_statement; 12, expression_statement; 13, expression_statement; 14, comment:# we have a directed ordering via ascending; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, return_statement; 20, identifier:level; 21, integer:0; 22, identifier:ascending; 23, True; 24, identifier:sort_remaining; 25, True; 26, comment:"""
          Sort MultiIndex at the requested level. The result will respect the
          original ordering of the associated factor at that level.
  
          Parameters
          ----------
          level : list-like, int or str, default 0
              If a string is given, must be a name of the level
              If list-like must be names or ints of levels.
          ascending : boolean, default True
              False to sort in descending order
              Can also be a list to specify a directed ordering
          sort_remaining : sort by the remaining levels after level
  
          Returns
          -------
          sorted_index : pd.MultiIndex
              Resulting index.
          indexer : np.ndarray
              Indices of output values in original index.
          """; 27, dotted_name; 28, dotted_name; 29, call; 30, block; 31, assignment; 32, assignment; 33, call; 34, block; 35, comment:# level ordering; 36, else_clause; 37, assignment; 38, assignment; 39, assignment; 40, expression_list; 41, identifier:pandas; 42, identifier:core; 43, identifier:sorting; 44, identifier:indexer_from_factorized; 45, identifier:isinstance; 46, argument_list; 47, expression_statement; 48, identifier:level; 49, list_comprehension; 50, identifier:sortorder; 51, None; 52, identifier:isinstance; 53, argument_list; 54, if_statement; 55, import_from_statement; 56, expression_statement; 57, block; 58, identifier:indexer; 59, call; 60, identifier:new_codes; 61, list_comprehension; 62, identifier:new_index; 63, call; 64, identifier:new_index; 65, identifier:indexer; 66, identifier:level; 67, tuple; 68, assignment; 69, call; 70, for_in_clause; 71, identifier:ascending; 72, identifier:list; 73, not_operator; 74, block; 75, dotted_name; 76, dotted_name; 77, assignment; 78, expression_statement; 79, expression_statement; 80, comment:# partition codes and shape; 81, expression_statement; 82, expression_statement; 83, comment:# Reverse sorted to retain the order of; 84, comment:# smaller indices that needs to be removed; 85, for_statement; 86, if_statement; 87, expression_statement; 88, if_statement; 89, identifier:ensure_platform_int; 90, argument_list; 91, call; 92, for_in_clause; 93, identifier:MultiIndex; 94, argument_list; 95, identifier:str; 96, identifier:int; 97, identifier:level; 98, list; 99, attribute; 100, argument_list; 101, identifier:lev; 102, identifier:level; 103, comparison_operator:len(level) == len(ascending); 104, raise_statement; 105, identifier:pandas; 106, identifier:core; 107, identifier:sorting; 108, identifier:lexsort_indexer; 109, identifier:indexer; 110, call; 111, assignment; 112, assignment; 113, assignment; 114, assignment; 115, identifier:lev; 116, call; 117, block; 118, identifier:sort_remaining; 119, block; 120, else_clause; 121, assignment; 122, not_operator; 123, block; 124, identifier:indexer; 125, attribute; 126, argument_list; 127, identifier:level_codes; 128, attribute; 129, keyword_argument; 130, keyword_argument; 131, keyword_argument; 132, keyword_argument; 133, keyword_argument; 134, identifier:level; 135, identifier:self; 136, identifier:_get_level_number; 137, identifier:lev; 138, call; 139, call; 140, call; 141, identifier:lexsort_indexer; 142, argument_list; 143, identifier:codes; 144, call; 145, identifier:shape; 146, call; 147, identifier:primary; 148, call; 149, identifier:primshp; 150, call; 151, identifier:sorted; 152, argument_list; 153, expression_statement; 154, expression_statement; 155, expression_statement; 156, expression_statement; 157, block; 158, identifier:indexer; 159, call; 160, identifier:ascending; 161, expression_statement; 162, identifier:level_codes; 163, identifier:take; 164, identifier:indexer; 165, identifier:self; 166, identifier:codes; 167, identifier:codes; 168, identifier:new_codes; 169, identifier:levels; 170, attribute; 171, identifier:names; 172, attribute; 173, identifier:sortorder; 174, identifier:sortorder; 175, identifier:verify_integrity; 176, False; 177, identifier:len; 178, argument_list; 179, identifier:len; 180, argument_list; 181, identifier:ValueError; 182, argument_list; 183, list_comprehension; 184, keyword_argument; 185, identifier:list; 186, argument_list; 187, identifier:list; 188, argument_list; 189, identifier:tuple; 190, generator_expression; 191, identifier:tuple; 192, generator_expression; 193, identifier:level; 194, keyword_argument; 195, call; 196, call; 197, augmented_assignment; 198, augmented_assignment; 199, expression_statement; 200, identifier:indexer_from_factorized; 201, argument_list; 202, assignment; 203, identifier:self; 204, identifier:levels; 205, identifier:self; 206, identifier:names; 207, identifier:level; 208, identifier:ascending; 209, string:"level must have same length as ascending"; 210, subscript; 211, for_in_clause; 212, identifier:orders; 213, identifier:ascending; 214, attribute; 215, attribute; 216, subscript; 217, for_in_clause; 218, subscript; 219, for_in_clause; 220, identifier:reverse; 221, True; 222, attribute; 223, argument_list; 224, attribute; 225, argument_list; 226, identifier:primary; 227, binary_operator:primary + tuple(codes); 228, identifier:primshp; 229, binary_operator:primshp + tuple(shape); 230, assignment; 231, identifier:primary; 232, identifier:primshp; 233, keyword_argument; 234, identifier:indexer; 235, subscript; 236, attribute; 237, identifier:lev; 238, identifier:lev; 239, identifier:level; 240, identifier:self; 241, identifier:codes; 242, identifier:self; 243, identifier:levshape; 244, identifier:codes; 245, identifier:lev; 246, identifier:lev; 247, identifier:level; 248, identifier:shape; 249, identifier:lev; 250, identifier:lev; 251, identifier:level; 252, identifier:codes; 253, identifier:pop; 254, identifier:lev; 255, identifier:shape; 256, identifier:pop; 257, identifier:lev; 258, identifier:primary; 259, call; 260, identifier:primshp; 261, call; 262, identifier:sortorder; 263, subscript; 264, identifier:compress; 265, False; 266, identifier:indexer; 267, slice; 268, identifier:self; 269, identifier:codes; 270, identifier:tuple; 271, argument_list; 272, identifier:tuple; 273, argument_list; 274, identifier:level; 275, integer:0; 276, unary_operator; 277, identifier:codes; 278, identifier:shape; 279, integer:1
code: def sortlevel(self, level=0, ascending=True, sort_remaining=True):
          """
          Sort MultiIndex at the requested level. The result will respect the
          original ordering of the associated factor at that level.
  
          Parameters
          ----------
          level : list-like, int or str, default 0
              If a string is given, must be a name of the level
              If list-like must be names or ints of levels.
          ascending : boolean, default True
              False to sort in descending order
              Can also be a list to specify a directed ordering
          sort_remaining : sort by the remaining levels after level
  
          Returns
          -------
          sorted_index : pd.MultiIndex
              Resulting index.
          indexer : np.ndarray
              Indices of output values in original index.
          """
          from pandas.core.sorting import indexer_from_factorized
  
          if isinstance(level, (str, int)):
              level = [level]
          level = [self._get_level_number(lev) for lev in level]
          sortorder = None
  
          # we have a directed ordering via ascending
          if isinstance(ascending, list):
              if not len(level) == len(ascending):
                  raise ValueError("level must have same length as ascending")
  
              from pandas.core.sorting import lexsort_indexer
              indexer = lexsort_indexer([self.codes[lev] for lev in level],
                                        orders=ascending)
  
          # level ordering
          else:
  
              codes = list(self.codes)
              shape = list(self.levshape)
  
              # partition codes and shape
              primary = tuple(codes[lev] for lev in level)
              primshp = tuple(shape[lev] for lev in level)
  
              # Reverse sorted to retain the order of
              # smaller indices that needs to be removed
              for lev in sorted(level, reverse=True):
                  codes.pop(lev)
                  shape.pop(lev)
  
              if sort_remaining:
                  primary += primary + tuple(codes)
                  primshp += primshp + tuple(shape)
              else:
                  sortorder = level[0]
  
              indexer = indexer_from_factorized(primary, primshp,
                                                compress=False)
  
              if not ascending:
                  indexer = indexer[::-1]
  
          indexer = ensure_platform_int(indexer)
          new_codes = [level_codes.take(indexer) for level_codes in self.codes]
  
          new_index = MultiIndex(codes=new_codes, levels=self.levels,
                                 names=self.names, sortorder=sortorder,
                                 verify_integrity=False)
  
          return new_index, indexer