Changing the Order of Rows

From Q
Jump to navigation Jump to search

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:

  1. Select your table.
  2. Select Automate > Custom Rule.
  3. Paste in the code from below.
  4. 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.
  5. 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