In Dynamics, to sort the data from the data source of a form you need to modify the init() method.
After super(), use the following code to sort by a field:
/// <summary>
/// Ovveride init method
/// </summary>
public void init()
{
super();
SalesTable_DS.query().dataSourceTable(tableNum(SalesTable)).addSortField(fieldNum(SalesTable, SalesId), SortOrder::Descending);
}
It is also possible to sort by multiple fields as follows:
/// <summary>
/// Ovveride init method
/// </summary>
public void init()
{
super();
SalesTable_DS.query().dataSourceTable(tableNum(SalesTable)).addSortField(fieldNum(SalesTable, SalesId), SortOrder::Descending);
SalesTable_DS.query().dataSourceTable(tableNum(SalesTable)).addSortField(fieldNum(SalesTable, DeliveryDate), SortOrder::Descending);
}