6. Working with Arrays, Table Fields, and Collections : Table Field Operations : Controlling Multiple Table Fields with a Single Control Button : How You Can Sort Table Field Data Generically
 
Share this page                  
How You Can Sort Table Field Data Generically
Sorting array data by specifying a column in its associated table field is a common operation. You can put a generic sort operation into a control button that is attached to a specific table field or is an independent control button field.
The following event block for a Control Button's Option Menu's Menu List lets users perform this operation, determining the names of the table field and column at runtime:
/* Script for MENULIST */
on click =

declare
   table_field = TableField default null;
   tf_array = ArrayObject default null;
   col_name = varchar(32) not null;
   sort_order = integer not null;
enddeclare
begin
/* Find out what table and column we are in,
** if any. */
if CurFrame.InputFocusField is not null then
   table_field =
   CurFrame.InputFocusField.WhichTableField();
endif;
if table_field is null then
   message 'You must be in a table field to sort
   it. ';
   resume;
endif;
col_name = CurFrame.InputFocusField.Name;
/* Point tf_array to the array underlying
** the tablefield */
table_field.GetFieldValue(value = byref(tf_array));
/*
** Use CurEnumValue to specify sort order.
** Note that the first item in the menu list
** is labeled sort in ascending order, and the
** second item to sort in descending order.
*/
if MenuList(CurFrame.TriggerField).CurEnumValue = 1
   then sort_order = AS_ASC;
else
   sort_order = AS_DESC;
endif;
/* Note the colon in front of col_name is required
** to indicate that col_name is a variable, not the
** literal column name.*/
tf_array.sort(:col_name = sort_order);
end;
For more information about the Sort method of the ArrayObject system class, see How Sorting Arrays Works (see How Sorting Arrays Works). For information about using the WhichTableField method of the TableField system class, see WhichTableField Method (see WhichTableField Method).