nodes: 0, module; 1, function_definition; 2, function_name:sort_values; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, expression_statement; 11, if_statement; 12, expression_statement; 13, if_statement; 14, identifier:inplace; 15, False; 16, identifier:ascending; 17, True; 18, identifier:na_position; 19, string; 20, comment:"""
          Sort the Categorical by category value returning a new
          Categorical by default.
  
          While an ordering is applied to the category values, sorting in this
          context refers more to organizing and grouping together based on
          matching category values. Thus, this function can be called on an
          unordered Categorical instance unlike the functions 'Categorical.min'
          and 'Categorical.max'.
  
          Parameters
          ----------
          inplace : bool, default False
              Do operation in place.
          ascending : bool, default True
              Order ascending. Passing False orders descending. The
              ordering parameter provides the method by which the
              category values are organized.
          na_position : {'first', 'last'} (optional, default='last')
              'first' puts NaNs at the beginning
              'last' puts NaNs at the end
  
          Returns
          -------
          Categorical or None
  
          See Also
          --------
          Categorical.sort
          Series.sort_values
  
          Examples
          --------
          >>> c = pd.Categorical([1, 2, 2, 1, 5])
          >>> c
          [1, 2, 2, 1, 5]
          Categories (3, int64): [1, 2, 5]
          >>> c.sort_values()
          [1, 1, 2, 2, 5]
          Categories (3, int64): [1, 2, 5]
          >>> c.sort_values(ascending=False)
          [5, 2, 2, 1, 1]
          Categories (3, int64): [1, 2, 5]
  
          Inplace sorting can be done as well:
  
          >>> c.sort_values(inplace=True)
          >>> c
          [1, 1, 2, 2, 5]
          Categories (3, int64): [1, 2, 5]
          >>>
          >>> c = pd.Categorical([1, 2, 2, 1, 5])
  
          'sort_values' behaviour with NaNs. Note that 'na_position'
          is independent of the 'ascending' parameter:
  
          >>> c = pd.Categorical([np.nan, 2, 2, np.nan, 5])
          >>> c
          [NaN, 2.0, 2.0, NaN, 5.0]
          Categories (2, int64): [2, 5]
          >>> c.sort_values()
          [2.0, 2.0, 5.0, NaN, NaN]
          Categories (2, int64): [2, 5]
          >>> c.sort_values(ascending=False)
          [5.0, 2.0, 2.0, NaN, NaN]
          Categories (2, int64): [2, 5]
          >>> c.sort_values(na_position='first')
          [NaN, NaN, 2.0, 2.0, 5.0]
          Categories (2, int64): [2, 5]
          >>> c.sort_values(ascending=False, na_position='first')
          [NaN, NaN, 5.0, 2.0, 2.0]
          Categories (2, int64): [2, 5]
          """; 21, assignment; 22, comparison_operator:na_position not in ['last', 'first']; 23, block; 24, assignment; 25, identifier:inplace; 26, block; 27, else_clause; 28, string_content:last; 29, identifier:inplace; 30, call; 31, identifier:na_position; 32, list; 33, expression_statement; 34, raise_statement; 35, identifier:sorted_idx; 36, call; 37, expression_statement; 38, block; 39, identifier:validate_bool_kwarg; 40, argument_list; 41, string; 42, string; 43, assignment; 44, call; 45, identifier:nargsort; 46, argument_list; 47, assignment; 48, return_statement; 49, identifier:inplace; 50, string; 51, string_content:last; 52, string_content:first; 53, identifier:msg; 54, string; 55, identifier:ValueError; 56, argument_list; 57, identifier:self; 58, keyword_argument; 59, keyword_argument; 60, attribute; 61, subscript; 62, call; 63, string_content:inplace; 64, string_content:invalid na_position: {na_position!r}; 65, call; 66, identifier:ascending; 67, identifier:ascending; 68, identifier:na_position; 69, identifier:na_position; 70, identifier:self; 71, identifier:_codes; 72, attribute; 73, identifier:sorted_idx; 74, attribute; 75, argument_list; 76, attribute; 77, argument_list; 78, identifier:self; 79, identifier:_codes; 80, identifier:self; 81, identifier:_constructor; 82, keyword_argument; 83, keyword_argument; 84, keyword_argument; 85, identifier:msg; 86, identifier:format; 87, keyword_argument; 88, identifier:values; 89, subscript; 90, identifier:dtype; 91, attribute; 92, identifier:fastpath; 93, True; 94, identifier:na_position; 95, identifier:na_position; 96, attribute; 97, identifier:sorted_idx; 98, identifier:self; 99, identifier:dtype; 100, identifier:self; 101, identifier:_codes
code: def sort_values(self, inplace=False, ascending=True, na_position='last'):
          """
          Sort the Categorical by category value returning a new
          Categorical by default.
  
          While an ordering is applied to the category values, sorting in this
          context refers more to organizing and grouping together based on
          matching category values. Thus, this function can be called on an
          unordered Categorical instance unlike the functions 'Categorical.min'
          and 'Categorical.max'.
  
          Parameters
          ----------
          inplace : bool, default False
              Do operation in place.
          ascending : bool, default True
              Order ascending. Passing False orders descending. The
              ordering parameter provides the method by which the
              category values are organized.
          na_position : {'first', 'last'} (optional, default='last')
              'first' puts NaNs at the beginning
              'last' puts NaNs at the end
  
          Returns
          -------
          Categorical or None
  
          See Also
          --------
          Categorical.sort
          Series.sort_values
  
          Examples
          --------
          >>> c = pd.Categorical([1, 2, 2, 1, 5])
          >>> c
          [1, 2, 2, 1, 5]
          Categories (3, int64): [1, 2, 5]
          >>> c.sort_values()
          [1, 1, 2, 2, 5]
          Categories (3, int64): [1, 2, 5]
          >>> c.sort_values(ascending=False)
          [5, 2, 2, 1, 1]
          Categories (3, int64): [1, 2, 5]
  
          Inplace sorting can be done as well:
  
          >>> c.sort_values(inplace=True)
          >>> c
          [1, 1, 2, 2, 5]
          Categories (3, int64): [1, 2, 5]
          >>>
          >>> c = pd.Categorical([1, 2, 2, 1, 5])
  
          'sort_values' behaviour with NaNs. Note that 'na_position'
          is independent of the 'ascending' parameter:
  
          >>> c = pd.Categorical([np.nan, 2, 2, np.nan, 5])
          >>> c
          [NaN, 2.0, 2.0, NaN, 5.0]
          Categories (2, int64): [2, 5]
          >>> c.sort_values()
          [2.0, 2.0, 5.0, NaN, NaN]
          Categories (2, int64): [2, 5]
          >>> c.sort_values(ascending=False)
          [5.0, 2.0, 2.0, NaN, NaN]
          Categories (2, int64): [2, 5]
          >>> c.sort_values(na_position='first')
          [NaN, NaN, 2.0, 2.0, 5.0]
          Categories (2, int64): [2, 5]
          >>> c.sort_values(ascending=False, na_position='first')
          [NaN, NaN, 5.0, 2.0, 2.0]
          Categories (2, int64): [2, 5]
          """
          inplace = validate_bool_kwarg(inplace, 'inplace')
          if na_position not in ['last', 'first']:
              msg = 'invalid na_position: {na_position!r}'
              raise ValueError(msg.format(na_position=na_position))
  
          sorted_idx = nargsort(self,
                                ascending=ascending,
                                na_position=na_position)
  
          if inplace:
              self._codes = self._codes[sorted_idx]
          else:
              return self._constructor(values=self._codes[sorted_idx],
                                       dtype=self.dtype,
                                       fastpath=True)