EPUB | CHM | PDF

TDataSet.Properties.SortFieldNames

Top Previous Next

Specifies field names and sorting order to sort opened dataset by these fields on the client side without refetching data from server.

Syntax:

property SortFieldNames : string;

Description:

Set SortFieldNames to establish or change the list of fields on which the dataset is sorted. Set sort to the name of a single field or to a comma-separated list of fields. Every field name can be followed by the keyword 'ASC' or 'DESC' to specify a sort direction for the field. If one of these keywords is not used, the default sort direction for the field is ascending ('ASC').

For example:

mySQLQuery1.SortFieldNames := 'ID, Name DESC, ColorValue ASC';

Since v2.6.3 double-quote character ( " ) can be used to quote field name if one contain spaces, commas or other non-alphanumeric character.

For example:

mySQLQuery1.SQL.Clear;
mySQLQuery1.SQL.Add('SELECT LEFT(TABLE_NAME, 20), TABLE_COLLATION FROM INFORMATION_SCHEMA.TABLES');
mySQLQuery1.Open;
mySQLQuery1.SortFieldNames := 'TABLE_COLLATION, "LEFT(TABLE_NAME, 20)" DESC';

If dataset is opened setting this property to some string causes sorting of dataset immediately. If dataset is closed (Active = False) it will be sorted by this fields after opening. This property can also be used at Design-time.

note Since v2.7.6 you can adjust case sensitivity of sorting using TMySQLDatabase.DatasetOptions property.

Example:

This code can be used to sort data in TDBGrid component by particular column when user clicks on its title.

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  //if column already sorted lets sort it in reverse order
  if mySQLTable1.SortFieldNames = Column.FieldName then
    mySQLTable1.SortFieldNames := Column.FieldName + ' DESC'
  else
    mySQLTable1.SortFieldNames := Column.FieldName;
end;

See also: SortBy method, TMySQLDatabase.DatasetOptions property