Reordering Variables

From Q
Jump to navigation Jump to search

This code moves the selected questions to the bottom of the file and then reorders them so that Q2 appears before Q1. It creates an array of the selected variables and then loops through the variables by name and adds them to a new array in the specified order. You can change which variables to move by entering the appropriate variable name in the 'selected_vars' line below.

var data_file = project.dataFiles[0];
var selected_vars = ["Q2","Q1"];
var moved_vars = [];

for (var i = 0; i < selected_vars.length; i++) {
    var curr_var = data_file.getVariableByName(selected_vars[i]);
    var variables = data_file.variables;
    data_file.moveAfter([curr_var],variables[variables.length-1]);
    moved_vars.push(curr_var);
}

See also