def sortlevel(self, level=<NUM_LIT:0>, ascending=True, sort_remaining=True):
from pandas.core.sorting import indexer_from_factorized<EOL>if isinstance(level, (str, int)):<EOL><INDENT>level = [level]<EOL><DEDENT>level = [self._get_level_number(lev) for lev in level]<EOL>sortorder = None<EOL>if isinstance(ascending, list):<EOL><INDENT>if not len(level) == len(ascending):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>from pandas.core.sorting import lexsort_indexer<EOL>indexer = lexsort_indexer([self.codes[lev] for lev in level],<EOL>orders=ascending)<EOL><DEDENT>else:<EOL><INDENT>codes = list(self.codes)<EOL>shape = list(self.levshape)<EOL>primary = tuple(codes[lev] for lev in level)<EOL>primshp = tuple(shape[lev] for lev in level)<EOL>for lev in sorted(level, reverse=True):<EOL><INDENT>codes.pop(lev)<EOL>shape.pop(lev)<EOL><DEDENT>if sort_remaining:<EOL><INDENT>primary += primary + tuple(codes)<EOL>primshp += primshp + tuple(shape)<EOL><DEDENT>else:<EOL><INDENT>sortorder = level[<NUM_LIT:0>]<EOL><DEDENT>indexer = indexer_from_factorized(primary, primshp,<EOL>compress=False)<EOL>if not ascending:<EOL><INDENT>indexer = indexer[::-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>indexer = ensure_platform_int(indexer)<EOL>new_codes = [level_codes.take(indexer) for level_codes in self.codes]<EOL>new_index = MultiIndex(codes=new_codes, levels=self.levels,<EOL>names=self.names, sortorder=sortorder,<EOL>verify_integrity=False)<EOL>return new_index, indexer<EOL>
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.