Sorting
Summary
- Column Sorting in AdapTable is configured through Layouts which contain full sorting information
- Additionaly AdapTable offers Custom Sorting whereby a bespoke sort order can be provided for a Column
- Custom Sorts can be supplied in 2 ways:
- as a hard coded list in Initial Adaptable State
- a function supplied in Adaptable Options
AdapTable fully supports sorting of your data in AG Grid.
Column Sorting
Column Sorting is managed through Layouts Sorting.
Each Layout includes a ColumnSorts which contains details of which Columns are sorted and in which order.
Hint
Like all Layout props this can be predefined, allowing developers to define the sorted columns when the Layout loads
Custom Sorting
Custom Sorting enables Adaptable Columns to be given a bespoke sort order.
This will be applied by AdapTable whenever the column is sorted in place of the column's default sort order.
Note
Custom Sorts are applied when a Column is sorted in AG Grid in both table and pivot Layouts
Custom Sorting is useful when the contents of a column are typically ordered in an non-standard way.
Deep Dive
Default Sorting Rules for each Column DataType
For example, a 'Ratings' column can be sorted by rating (i.e. 'AAA', 'AA1' etc) rather than alphabetically (the default for string columns).
A Custom Sort can be one of 2 types:
- Hard-coded List of Values - provided in Custom Sort Initial State (or via the UI)
- Comparer function - provided in Custom Sort Options, invoked by AdapTable each time the column's sorted
Caution
- Only one Custom Sort can be supplied per Column
- Custom Sort Comparers take precedence over Hard-coded Lists
Defining Custom Sorts
At design time developers can configure both types of Custom Sorts.
Hardcoded Values
Custom Sorts can be provided using a list of values in the Custom Sort section of Initial Adaptable State.
AdapTable will sort the Column according to the order of the supplied values.
Note
- Its likely the Column will contain values that are not included in the Custom Sort implementation list
- AdapTable will sort them according to the default alphabetic order
Developer Guide
Defining a Custom Sort with Hardcoded Values
const initialState: InitialState = {
CustomSort: {
CustomSorts: [
{
Name: 'CustomSort-Rating',
ColumnId: 'Rating',
SortedValues: ['AAA', 'AA+', 'AA', 'AA-'], // etc.
},
],
},
} as InitialState;Add a CustomSort section containing an array of CustomSort objects.
Choose a unique name for the Custom Sort
Specify the Id of Column which will use the Custom Sort
Supply an ordered list by which the column will be sorted
Note
Values not provided are sorted using Column's default sort order
Comparer Function
For more complicated scenarios a customSortComparer function can be supplied by developers.
This comparer function will be invoked by AdapTable each time the Column is sorted.
Comparer functions are provided in the customSortComparers property of Custom Sort Options.
customSortComparers
Hint
- Unlike with Hardcoded Values the
customSortComparerfunction can be given Column Scope - This allows, for example, for a single
customSortComparerfunction to be provided for all Date columns
Developer Guide
Defining a Custom Sort using a CustomSortComparer function
Just a few steps are required to define a Custom Sort Comparer.
In this example, we provide a Comparer which will ignore negatives and sort by absolute value
// Sort Issue Change by size of change (ignoring negatives)
customSortOptions : {
customSortComparers: [
{
name: 'absolute_sort',
scope: {
DataTypes: ['number']
},
comparer: (valA: any, valB: any: IRowNode)
=> { return Math.abs(valB) - Math.abs(valA);
},
}:
]
};Add customSortComparers property to Custom Sort Options
Provide a unique name for the Comparer
The commonly-used Column Scope object defines where the Custom Sort Comparer will run. It can be:
- One or more ColumnIds
- A Cell Data Type (e.g. text, date, number)
- A Column Type
The function receives 2 cell values (and their containing row nodes)
The evaluation returns -1, 1 or 0 based on comparison being made
- This demo contains 3 Columns with Custom Sorts provided via a
customSortComparerFunction:Issue Change- thecustomSortComparernegates the numbers to order changes by size (we sort on this by default in the Layout)Name- thecustomSortComparerorders the column according to the value in theGitHub StarscolumnRating- thecustomSortCompareruses thelocaleComparefunction (so it orders a, A, b, B, c, C etc)
- Sort on the
Namecolumn to see how its sorted according to theGitHub Starscolumn value - Sort on the
Ratingcolumn to see how the comparer function sorts ignoring case
Using Custom Sorts
Run-time users can create, edit, delete, share and suspend Custom Sorts.
This is doing using the Custom Sort section of the Settings Panel.
Caution
- The AdapTable UI only deals with List-based Custom Sorts
- Custom Sort Comparers are design-time objects only and not editable, shareable or suspendable at run-time