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
FromUnit_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:Visual Indicators
TheTSortableHeader 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
- User clicks column header
OnCanSort- Check if column is sortableOnSortBy- Perform the sortOnSortState- 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
- Implement OnCanSort to control which columns can be sorted
- Track sort state to toggle between ascending/descending
- Use TComparer for clean, reusable sort logic
- Sort in place when possible for best performance
- Refresh grid after sorting (usually automatic)
Next Steps
- Learn about Database Grid for database sorting
- Explore Themes to customize sort indicators
- See Performance for large dataset sorting
