function [y,ndx] = sortrows(y,varargin)
%MATLAB Code Generation Library Function
%
% Limitations:
%
%   1. Cell input for first argument is not supported

%   Copyright 1984-2020 The MathWorks, Inc.
%#codegen

coder.internal.allowEnumInputs;
coder.internal.prefer_const(varargin);
narginchk(1,7);
coder.internal.assert(~iscell(y), ...
    'Coder:toolbox:CellArraysNotSupported',mfilename);
coder.internal.assert(coder.internal.isBuiltInNumeric(y) || ...
    ischar(y) || islogical(y), ...
    'Coder:toolbox:unsupportedClass','sortrows',class(y));
coder.internal.assert(isreal(y) || isa(y,'float'), ...
    'MATLAB:sortrowsc:ComplexIntegers');
coder.internal.assert(ismatrix(y), ...
    'MATLAB:sortrows:inputDimensionMismatch');
n = coder.internal.indexInt(size(y,2));
[col,comparisonMethod,missingPlacement] = ...
    coder.internal.parseSortrowsOptions(n,varargin{:});
% Compute the row permutation vector.
idx = coder.internal.sortIdx(y,col,comparisonMethod,missingPlacement);
y = apply_row_permutation(y,idx);
ndx = double(idx(:));

%--------------------------------------------------------------------------

function y = apply_row_permutation(y,idx)
% Permute the rows of matrix Y using the permutation vector IDX.
m = cast(size(y,1),coder.internal.indexIntClass);
n = cast(size(y,2),coder.internal.indexIntClass);
ycol = coder.nullcopy(eml_expand(coder.internal.scalarEg(y),[m,1]));
for j = 1:n
    for i = 1:m
        ycol(i) = y(idx(i),j);
    end
    for i = 1:m
        y(i,j) = ycol(i);
    end
end

%--------------------------------------------------------------------------
