How to write code for sorting field in a grid

Assuming you are trying to sort field CustGroup on datasource CustTable:

1) add ComboBox on form, set Name=ComboSortOrder, AutoDeclaration=Yes and EnumType=SortOrder

2) override modified() method on ComboBox form control to call executeQuery() of the datasource

public boolean modified()
{
boolean ret;
;
ret = super();

CustTable_ds.executeQuery();

return ret;
}

3) override executeQuery() method on datasource to change sortorder before actual fetch of records

public void executeQuery()
{;
CustTable_ds.query().dataSourceNo(1).sortClear();
CustTable_ds.query().dataSourceNo(1).addSortField(fieldNum(CustTable,
CustGroup), ComboSortOrder.selection()); // you can change that to //CustTable_ds.query().dataSourceNo(1).addSortField(fieldNum(CustTable,
//CustGroup), SortOrder::Ascending); //If you want to display fields in an ascending order.

super();
}
Notice There is a sort method on each control, overriding which you can influence the actual actions which happed when you click on the header of a column