Changing the Order of Rows
Jump to navigation
Jump to search
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript > Table JavaScript and Plot JavaScript Examples Library
In some cases you may want to use a specific ordering of brands or statements in the rows of a table, and it may not be efficient to modify the Questions (for example, if you have tens or hundreds of Questions to order). The code below specifies the order in which the rows on a table should appear.
To use this snippet:
- Select your table.
- Select Automate > Custom Rule.
- Paste in the code from below.
- Change the labels in the first line to match the labels and order that you want to use for ordering the rows in your table.
- Click the 'play' icon and close.
The rule will warn you if it can't match any of the labels in your table.
This rule will not work if you have any duplicate labels in the table.
var row_order = ["Pepsi ", "Coca Cola ", "NET Sugarred", "Pepsi Light ", "Pepsi Max", "Diet Coke", "Coke Zero", "NET Sugarless", "NET"];
var row_labels = table.rowLabels;
var not_found_labels = row_order.filter(function (label) { return row_labels.indexOf(label) == -1; })
if (not_found_labels.length > 0) {
log("Could not find labels in the rows of the table:\r\n" + not_found_labels.join("\r\n"));
} else {
var last;
row_order.forEach(function (label, ind) {
if (ind == 0) {
table.moveRowAfter(table.rowIndex(label), null);
} else {
table.moveRowAfter(table.rowIndex(label), table.rowIndex(last));
}
last = label;
});
}
See also
- Table JavaScript and Plot JavaScript for an explanation of how to run this code.
- Table JavaScript and Plot JavaScript Reference for technical information.
- Table JavaScript and Plot JavaScript Examples Library for other examples.
- JavaScript for information about the JavaScript programming language.
- QScript for tools for automating projects using JavaScript.
- JavaScript Variables for detail on how to create new variables in the Variables and Questions tab using JavaScript.
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript > Table JavaScript and Plot JavaScript Examples Library