function [y,ndx] = sortrows(x,col)
%SORTROWS Sort rows in ascending order.
%   SORTROWS(X) sorts the rows of the matrix X in ascending order as a
%   group.  For ASCII strings, this is the familiar dictionary sort.
%   When X is complex, the elements are sorted by ABS(X). Complex
%   matches are further sorted by ANGLE(X).
%
%   SORTROWS(X,COL) sorts the matrix based on the columns specified in
%   the vector COL.  For example, SORTROWS(X,[2 3]) sorts the rows of X
%   by the second and third columns of X.
%
%   [Y,I] = SORTROWS(X) also returns an index matrix I such that Y = X(I,:).
%
%   See also SORT.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.13 $  $Date: 2000/06/01 02:10:40 $

if nargin<1, error('Not enough input arguments.'); end
if ndims(x)>2, error('X must be a 2-D matrix.'); end

[m,n] = size(x);
if nargin<2, col = 1:n; end

% Sort back to front
if m>0
  ndx = (1:m)';
else
  ndx = [];
end

for i=length(col):-1:1,
  [v,ind] = sort(x(ndx,col(i)));
  ndx = ndx(ind);
end
y = x(ndx,:);
