Skip to main content

Overview

TeeGrid supports custom column sorting by clicking on column headers. You can implement your own sort logic for any data type or structure.

Basic Sortable Header

Enabling Sorting

Creating the Sortable Header

Implementing Sort Events

OnCanSort - Control Which Columns Are Sortable

OnSortState - Return Current Sort State

OnSortBy - Perform the Sort

Custom Sort Implementation

Using TComparer for Arrays

Complete Example

From Unit_Custom_Sorting.pas:

Automatic Sorting with TeeBI

For automatic sorting without custom code, see the TeeBI demos. The BI.GridData.pas unit provides built-in sorting functionality.

Database Sorting

For database grids, use the sortable dataset helpers:
See the Database Grid example for details.

Visual Indicators

The TSortableHeader automatically displays:
  • ▲ Triangle pointing up for ascending sort
  • ▼ Triangle pointing down for descending sort
  • No indicator for unsorted columns

Key Concepts

TSortState Enum

Sort Events Flow

  1. User clicks column header
  2. OnCanSort - Check if column is sortable
  3. OnSortBy - Perform the sort
  4. OnSortState - Return current state (for visual indicator)

Custom Comparer Pattern

Advanced Usage

Multi-Column Sort

Case-Sensitive Sort

Numeric Sort

Features Demonstrated

  • Enabling sortable column headers
  • Custom sort logic with TComparer
  • Visual sort indicators (▲▼)
  • Controlling which columns are sortable
  • Toggling between ascending/descending
  • Sorting arrays in place

Best Practices

  1. Implement OnCanSort to control which columns can be sorted
  2. Track sort state to toggle between ascending/descending
  3. Use TComparer for clean, reusable sort logic
  4. Sort in place when possible for best performance
  5. Refresh grid after sorting (usually automatic)

Next Steps