Sorting records in Delphi datasets

Delphi FireDAC datasets offer a quick and easy sorting feature, through the property “IndexFieldNames” in which you list the field names to use as an index, and you can optionally specify sorting options for each field.

This property is an alternative for creating and setting indexes for a dataset in the property “IndexName”.

Note that “IndexFieldNames” and “IndexName” properties are mutually exclusive, so setting one property will clear the other.

How to use it

  • Whether in the object inspector or through code, you specify the name of each field for indexing the dataset, separated with semi-colons.
  • Field names order is significant for the sort.
  • Optionally, you can specify the sort options for any field in your list, by using the following syntax explained in the table below :
fieldname[:[D][A][N]]
DUse descending sorting on this field
AUse ascending sorting on this field
NUse case-insensitive sorting on this field
Sorting options syntax in the “IndexFieldNames” property

Examples

Using many fields for sorting with no options :

FDMemTable1.IndexFieldNames := 'Speciality;Subject;Title;Author;Publisher;Year';

Setting options for a field :

FDQuery1.IndexFieldNames := 'order_date:D;customer_name:N';

See also

Leave a Reply