gusucode.com > datatypes 工具箱matlab源码程序 > datatypes/@categorical/sort.m

    function [b,varargout] = sort(a,varargin)
%SORT Sort a categorical array.
%   B = SORT(A), when A is a categorical vector, sorts the elements of A in
%   ascending order.  For matrices, SORT(A) sorts each column of A in ascending
%   order. For N-D arrays, SORT(A) sorts along the first non-singleton
%   dimension of A.  B is a categorical array with the same categories as A.
%
%   B = SORT(A,DIM) sorts A along dimension DIM.
%
%   B = SORT(...,MODE) sorts A in the order specified by MODE.  MODE is
%   'ascend' for ascending order, or 'descend' for descending order.
%
%   [B,I] = SORT(A,...) also returns an index matrix I.  If A is a
%   vector, then B = A(I).  If A is an m-by-n matrix and DIM = 1, then
%   B(:,j) = A(I(:,j),j) for j = 1:n.
%
%   Undefined elements are treated as larger than all other elements.
%
%   See also ISSORTED, SORTROWS.

%   Copyright 2006-2015 The MathWorks, Inc. 

import matlab.internal.categoricalUtils.categoricalsort;

acodes = a.codes;
nCategories = numel(a.categoryNames);

try
    % CATEGORICALSORT uses counting sort for better performance when there
    % are fewer than 500,000 categories
    if nCategories <= 5e5
        if nargin == 1
            [bcodes,varargout{1:nargout-1}] = categoricalsort(acodes,nCategories);
        else
            [bcodes,varargout{1:nargout-1}] = categoricalsort(acodes,nCategories,varargin{:});
        end
    else % dispatch to builtin SORT
        % Make sure <undefined> sorts to the end when calling builtin SORT
        acodes(acodes == categorical.undefCode) = invalidCode(acodes);
        if nargin == 1
            [bcodes,varargout{1:nargout-1}] = sort(acodes);
        else
            [bcodes,varargout{1:nargout-1}] = sort(acodes,varargin{:});
        end
        bcodes(bcodes == invalidCode(bcodes)) = a.undefCode; % set invalidCode back to <undefined> code
    end
catch ME
    throw(ME);
end

b = a; % preserve subclass
b.codes = bcodes;