Purpose
Display a sort indicator in a table column header

We implement the possibility to sort a table by a column, indicating the sorting by a symbol in the column header.

Solution

We start with the following table:

Table (3,6) (10,40) name="mat" rows="12" fixedColumns=3
Column "Material" size=10 name="matnr"
Column "Amount" size=10 name="amount"
Column "Unit" size=10 name="unit"

We add two buttons to sort the table in ascending or descending order; the user may select a column and then press one of the buttons. In addition, we add variables to the column headers that we can fill with a symbol (special Unicode character) to indicate the ordering:

// table with column selection and sort indicator variables
Table (3,6) (10,40) name="mat" rows="10" -singleColumnSelection fixedColumns=3
Column "Material &V[mat.stat.sortsymbol_matnr]" size=10 name="matnr"
Column "Amount &V[mat.stat.sortsymbol_amount]" size=10 name="amount"
Column "Unit &V[mat.stat.sortsymbol_unit]" size=10 name="unit"

// buttons to sort the table
Box (3,44) (5,55) "Sort table"
Pushbutton (4,46) "@3E\QSort ascending@" size=(1,2) process="sort_table.txt"
  using MODE = "ASC"
Pushbutton (4,52) "@3F\QSort descending@" size=(1,2) process="sort_table.txt"
  using MODE = "DES"

InputScript "sort_table.txt":

// Sort table and display sort indicators
Parameter MODE // ASC or DES

// reset sort indicators
Clear V[mat.stat.sortsymbol_*]

// determine selected column
Clear V[current_column]
if
V[mat.stat.columnselection.matnr]
  Set V[current_column] "matnr"
endif
if
V[mat.stat.columnselection.amount]
  Set V[current_column] "amount"
endif
if
V[mat.stat.columnselection.unit]
  Set V[current_column] "unit"
endif

// no column selected? then take first one (material number)
if not V[current_column]
  Set V[current_column] "matnr"
endif

// set sort indicator symbol and sort table by selected column
if U[MODE=ASC]
  Set V[mat.stat.sortsymbol_&V[current_column]] ""
  Sort table="mat" orderBy="&V[current_column]"
else
  Set V[mat.stat.sortsymbol_&V[current_column]] ""
  Sort table="mat" orderBy="&V[current_column]" -descending
endif

// Reset column selection
Clear V[mat.stat.columnselection.*]

// Return with success message
Return
"S: Table sorted" -statusline

Components
InputAssistant