EPUB | CHM | PDF

TDataSet.Methods.SortBy

Top Previous Next

Sorts opened dataset on client side without refetching data from server.

Syntax:

procedure SortBy(FieldNames : string);

Description:

Call SortBy to sort dataset by particular fields on client side. Field names are case-sensitive and separated by commas. 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.SortBy('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.SortBy('TABLE_COLLATION, "LEFT(TABLE_NAME, 20)" DESC');

This method affects dataset order only when it is opened, i.e. when Active = True.

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 it title.

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
   mySQLTable1.SortBy(Column.FieldName + ' ASC');
end;

See also: SortFieldNames, TMySQLDatabase.DatasetOptions properties