function [sortedOut, org_index_r, org_index_c] =sort_right(matrix, cross)

if any(cross <0)
    rowSum = sum(matrix == -1,2);
    [~,org_index_r] = sort(rowSum,'ascend');
    matrix=matrix((org_index_r),:);
    
    columnSum=sum(matrix == -1);
    [~, org_index_c]=sort(columnSum, 'ascend');
    sortedOut=matrix(:,(org_index_c));
else
    rowSum = sum(matrix == 1,2);
    [~,org_index_r] = sort(rowSum,'ascend');
    matrix=matrix((org_index_r),:);
    
    columnSum=sum(matrix == 1);
    [~, org_index_c]=sort(columnSum, 'ascend');
    sortedOut=matrix(:,(org_index_c));
    
    
end

end