%
% SORTC  Sort in ascending order.
%        SORTC(X) sorts each column of X in ascending order. 
%        The elements are sorted by ABS(X).
%        Complex conjugate numbers are sorted with positiv 
%        imaginary part above the number with negative imaginary
%        part. [Y,I] = SORTC(X) also returns an index matrix I.
%        If X is a vector, then Y = X(I).
%        If X is an m-by-n matrix, then
%            for j = 1:n, Y(:,j) = X(I(:,j),j).
% 
%        See also MIN, MAX, SORT.
%

function [Xs,Is] = sortc(X,epp)

   if nargin < 2
      epp = 1e-12;
   end

%   [Xs,Is] = sort(X);

   for c=1:size(size(X,2))
      [Cs,Ics] = sort(abs(X(:,c)));
      Xs(:,c) = X(Ics,c);
      Is(:,c) = Ics;
   end
   
   for c=1:size(Xs,2)
      for r=1:size(Xs,1)-1
         if (isreal(Xs(r,c))==0) & (imag(Xs(r,c))<0) &...
            (abs(Xs(r,c)-cjt(Xs(r+1,c)))<epp) 
               
            H = Xs(r,c); 
            Xs(r,c)   = Xs(r+1,c);
            Xs(r+1,c) = H;
          
            H = Is(r,c); 
            Is(r,c)   = Is(r+1,c);
            Is(r+1,c) = H;

         end
      end
   end
end
