Using the template table i am able to add common columns but not in the right order defined in the template table.

What changes I need to make in this template script to full fill my requirement.
// columns are found by column name
// allowing reuse of already existing columns
// dynamic property ctemplateID is set afterwards - will keep connection to template
// column even if the name of column is changed
var t_name = "table_template";
var p_name = "ctemplateID";
template = model.getTableSet().getByName(t_name);
if(template!=null){
tcolumns = template.getElements();
tables = model.getTableSet().toArray();
for (var t = 0; t\<tables.length;t++){
table = tables\[t\];
// compare name ignoring the case
if(!table.getName().equalsIgnoreCase(t\_name)){
for (var i = 0; i \< tcolumns.length; i++) {
column = tcolumns\[i\];
col = table.getElementByName(column.getName());
if(col==null){
col = table.getColumnByProperty(p\_name,column.getObjectID());
}
if(col==null){
col = table.createColumn();
}
column.copy(col);
//set property after copy otherwise it'll be cleared by copy
col.setProperty(p\_name,column.getObjectID());
table.setDirty(true);
}
}
}
}