def _sort_levels_monotonic(self):
if self.is_lexsorted() and self.is_monotonic:<EOL><INDENT>return self<EOL><DEDENT>new_levels = []<EOL>new_codes = []<EOL>for lev, level_codes in zip(self.levels, self.codes):<EOL><INDENT>if not lev.is_monotonic:<EOL><INDENT>try:<EOL><INDENT>indexer = lev.argsort()<EOL><DEDENT>except TypeError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>lev = lev.take(indexer)<EOL>indexer = ensure_int64(indexer)<EOL>ri = lib.get_reverse_indexer(indexer, len(indexer))<EOL>level_codes = algos.take_1d(ri, level_codes)<EOL><DEDENT><DEDENT>new_levels.append(lev)<EOL>new_codes.append(level_codes)<EOL><DEDENT>return MultiIndex(new_levels, new_codes,<EOL>names=self.names, sortorder=self.sortorder,<EOL>verify_integrity=False)<EOL>
.. versionadded:: 0.20.0

This is an *internal* function.

Create a new MultiIndex from the current to monotonically sorted
items IN the levels. This does not actually make the entire MultiIndex
monotonic, JUST the levels.

The resulting MultiIndex will have the same outward
appearance, meaning the same .values and ordering. It will also
be .equals() to the original.

Returns
-------
MultiIndex

Examples
--------

>>> i = pd.MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
                      codes=[[0, 0, 1, 1], [0, 1, 0, 1]])
>>> i
MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
           codes=[[0, 0, 1, 1], [0, 1, 0, 1]])

>>> i.sort_monotonic()
MultiIndex(levels=[['a', 'b'], ['aa', 'bb']],
           codes=[[0, 0, 1, 1], [1, 0, 1, 0]])