By default, users can sort the row of a DataGrid by
clicking the column headers. Clicking the column header initially
sorts the display in descending order of the entries in the selected
column, and clicking the header again reverses the sort order. For
an example, see Sorting the columns of the Spark DataGrid control.
The GridColumnsortCompareFunction property
lets you specify a custom comparison function used to sort the rows
by that column. This property sets the compareFunction property
of the default Spark SortField class object used by the DataGrid.
The sortCompareFunction property lets you specify
the function that compares two objects and determines which would
be higher in the sort order, without requiring you to explicitly
create a Sort object on your data provider.
The comparison function passed to the sortCompareFunction property must
have the following signature:
function sortCompareFunction(obj1:Object, obj2:Object, gc:GridColumn):int {
// Sort logic
}
The obj1 and obj2 arguments
specify the objects to compare, and gc specifies the
column of the DataGrid control to sort.
Flex ships with two collator classes to help you write your comparison
function. The spark.globalization.MatchingCollator and spark.globalization.SortingCollator provide
locale-sensitive string comparison functions.
By default, the sorting used by the DataGrid control is case
sensitive. In the following example, you use the SortingCollator
class to perform a case-insensitive comparison of two cells to sort
the Name column of the DataGrid: