Custom Sorting of Rows in a Table

From Q
Jump to navigation Jump to search

This example uses JavaScript to sort the rows of a table according to a custom sort order based on the row labels.

This example can be run in C:\Program Files\Q\Examples\Cola.Q (this may be located on a different place on your computer depending upon how Q was installed). Create a table of Q5. Brand associations by SUMMARY.

var labels = ['Pepsi Max','Coke Zero','Pepsi Light','Diet Coke','Pepsi','Coca Cola'];
var last_index = null;

for (var i = 0; i < labels.length; i++) {
    
    table.moveRowAfter(table.rowIndex(labels[i]), last_index);
    last_index = table.rowIndex(labels[i]);
}

Note that:

  • The order of the rows is specified in the array in the first line of the code.
  • The labels must match what is shown in the table exactly.
  • The function table.moveRowAfter() is used to move each row. For more information and examples, see Making One Row Appear After Another.
  • A for loop is used to iterate over the set of specified labels.

See also