function [y,Y]=ordena(x,X,s);

n=length(x(:,1));
L=length(s(1,:));
P=permugen([1:n]);
I=eye(n);

for k=1:length(P(:,1))
    for ind=1:n
        M(ind,:,k)=I(P(k,ind),:);
    end
end
for k=1:n
    aux(k,:)=x(k,1:L);
end
clear x;
x=aux;
clear aux;
for k=1:n
    aux(:,:,k)=M(:,:,k)*x;
    error(k,:)=sum(sum((abs(aux(:,:,k)-s)).'));
end

ind=find(error==min(error));
y=M(:,:,ind)*x;

for k1=1:n
    for k2=1:n
        ind=2^(n-k2)*M(k1,k2);
    end
    ind=max(n,ind);
    Y(:,:,k1)=X(:,:,ind);
end