function [elems_ordered_filter, nodes_x_order_filter,...
    nodes_y_order_filter,elems_remain] =...
    element_order(min_index,bndry_elements,bndry_nodes_x, bndry_nodes_y)
%Takes input of list of DFlow elements and places them in order according
%to shared boundaries
%   Detailed explanation goes here

[elem_count,elem_variables] = size(bndry_elements);

elems_ordered = zeros(elem_count, elem_variables);
nodes_x_order = zeros(elem_count,8);
nodes_y_order = zeros(elem_count,8);

elems_ordered(1,:) = bndry_elements(min_index,:);
nodes_x_order(1,:) = bndry_nodes_x(min_index,:);
nodes_y_order(1,:) = bndry_nodes_y(min_index,:);

if min_index == 1
    elems_remain = bndry_elements(2:elem_count,:);
    nodes_x_remain = bndry_nodes_x(2:elem_count,:);
    nodes_y_remain = bndry_nodes_y(2:elem_count,:);
elseif min_index == elem_count
    elems_remain = bndry_elements(1:min_index-1,:);
    nodes_x_remain = bndry_nodes_x(1:min_index-1,:);
    nodes_y_remain = bndry_nodes_y(1:min_index-1,:);
elseif (min_index > 1) && (min_index < elem_count)
    elems_remain = bndry_elements([1:min_index-1,...
        min_index+1:elem_count],:);
    nodes_x_remain = bndry_nodes_x([1:min_index-1,...
        min_index+1:elem_count],:);
    nodes_y_remain = bndry_nodes_y([1:min_index-1,...
        min_index+1:elem_count],:);
end

skip = 0;
i = 0;

while i < elem_count
    
    i = i + 1;
    
    %fprintf('%d\r',i)
    
    if skip == 0;
    
        sides = nodes_x_order(i,3);
        node_x = nodes_x_order(i,4:4+sides-1);
        node_y = nodes_y_order(i,4:4+sides-1);
        [remain_count,~] = size(elems_remain);
        
    elseif skip == 1;
        
        sides = nodes_x_order(i-1,3);
        node_x = nodes_x_order(i-1,4:4+sides-1);
        node_y = nodes_y_order(i-1,4:4+sides-1);
        [remain_count,~] = size(elems_remain);
        
    elseif skip == 2 && extra_check == 1;
        
        sides = nodes_x_order(i-2,3);
        node_x = nodes_x_order(i-2,4:4+sides-1);
        node_y = nodes_y_order(i-2,4:4+sides-1);
        [remain_count,~] = size(elems_remain);
        
    elseif skip == 2 && extra_check == 2;
        
        break
        
    end
    
    for j = 1:remain_count
        
        match = 0;
            
        step_sides = nodes_x_remain(j,3);
        step_x = nodes_x_remain(j,4:4+step_sides-1);
        step_y = nodes_y_remain(j,4:4+step_sides-1);

        for k = 1:sides

            for l = 1:step_sides

                if (node_x(1,k) == step_x(1,l)) &&...
                        (node_y(1,k) == step_y(1,l))
                    match = match + 1;
                    break
                end

            end

        end

        if match > 1
            
            skip = 0;
            extra_check = 0;
            elems_ordered(i+1,:) = elems_remain(j,:);
            nodes_x_order(i+1,:) = nodes_x_remain(j,:);
            nodes_y_order(i+1,:) = nodes_y_remain(j,:);            
            
            if j == 1
                step_elems_remain = elems_remain(2:remain_count,:);
                step_nodes_x_remain = nodes_x_remain(2:remain_count,:);
                step_nodes_y_remain = nodes_y_remain(2:remain_count,:);
            elseif j == remain_count
                step_elems_remain = elems_remain(1:j-1,:);
                step_nodes_x_remain = nodes_x_remain(1:j-1,:);
                step_nodes_y_remain = nodes_y_remain(1:j-1,:);
            else
                step_elems_remain = elems_remain([1:j-1,...
                    j+1:remain_count],:);
                step_nodes_x_remain = nodes_x_remain([1:j-1,...
                    j+1:remain_count],:);
                step_nodes_y_remain = nodes_y_remain([1:j-1,...
                    j+1:remain_count],:);
            end
 
            clear elems_remain nodes_x_remain nodes_y_remain
            
            elems_remain = step_elems_remain;
            nodes_x_remain = step_nodes_x_remain;
            nodes_y_remain = step_nodes_y_remain;
            
            break
        
        elseif match <= 1 && j == remain_count && extra_check == 0
            
            skip = 1;
            elems_ordered(i,:) = 0;
            nodes_x_order(i,:) = 0;
            nodes_y_order(i,:) = 0;
            step_sides = 0;
            step_x = 0;
            step_y = 0;
            i = i - 1;
            extra_check = 1;
            
        elseif match <= 1 && j == remain_count && extra_check == 1;
            
            skip = 2;
            elems_ordered(i-1,:) = 0;
            nodes_x_order(i-1,:) = 0;
            nodes_y_order(i-1,:) = 0;
            step_sides = 0;
            step_x = 0;
            step_y = 0;
            i = i - 1;
            extra_check = 2;
        
        end
        
        clear step_sides step_x step_y
        
    end
    
    clear sides node_x node_y
    
end

elems_ordered_filter(:,:) = elems_ordered(elems_ordered(:,1)~=0,:);
nodes_x_order_filter(:,:) = nodes_x_order(elems_ordered(:,1)~=0,:);
nodes_y_order_filter(:,:) = nodes_y_order(elems_ordered(:,1)~=0,:);

end

