Sorting records

From InterBase

Go Up to Working with Tables

An index determines the display order of records in a table. In general, records appear in ascending order based on a primary index. This default behavior does not require application intervention. If you want a different sort order, however, you must specify either

  • An alternate index.
  • A list of columns on which to sort.

Specifying a different sort order requires the following steps:

  1. Determining available indexes.
  2. Specifying the alternate index or column list to use.

Retrieving a list of available indexes with GetIndexNames

At runtime, your application can call the GetIndexNames method to retrieve a list of available indexes for a table. GetIndexNames returns a string list containing valid index names. For example, the following code determines the list of indexes available for the CustomersTable dataset:

  IndexList: TList;

Specifying an alternative index with IndexName

To specify that a table should be sorted using an alternative index, specify the index name in the table component’s IndexName property. At design time you can specify this name in the Object Inspector, and at runtime you can access the property in your code. For example, the following code sets the index for CustomersTable to CustDescending:

CustomersTable.IndexName := 'CustDescending';

Specifying sort order for SQL tables

In SQL, sort order of rows is determined by the ORDER BY clause. You can specify the index used by this clause either with the

  • IndexName property, to specify an existing index, or
  • IndexFieldNames property, to create a pseudo-index based on a subset of columns in the table.

IndexName and IndexFieldNames are mutually exclusive. Setting one property clears values set for the other.

Advance To: