TcxCustomDataController.DefaultCompare(Integer,Integer,Integer) Method
Executes the default comparison routine for two specified values in a dataset field.
#Declaration
#Parameters
Name | Type | Description |
---|---|---|
ARecord |
Integer | Specifies the index of the record that contains the first compared value. The |
ARecord |
Integer | Specifies the index of the record that contains the second compared value. The |
AItem |
Integer | Specifies the index of the source dataset field where both compared values are stored.
|
#Returns
Type | Description |
---|---|
Integer | The comparison result. Refer to the following section for details: Returned Values. |
#Remarks
Call the DefaultCompare
function within an OnCompare event handler if you need to use the default comparison algorithm instead of a custom sorting algorithm under specific conditions.
Important
If your custom sorting algorithm is not thread-safe, make sure that multi-threaded calculations are disabled for data sort operations in the data controller.
To disable multi-threading for sort operations in the data controller, set the Multi
#Returned Values
Returned Value | Description |
---|---|
-1 |
ARecord is positioned before ARecord . |
0 |
ARecord and ARecord are considered equal. Both values retain their positions. |
1 |
ARecord is positioned after ARecord . |
#Code Example: Implement a Custom Date Comparison Algorithm
The code example below demonstrates an OnCompare event handler that groups records with the same month in the Sale Date column when records are sorted against this column. The event handler calls the DefaultCompare
function to execute the default comparison routine in all other cases.
procedure TForm1.MyOnCompareEventHandler(ADataController: TcxCustomDataController;
ARecordIndex1, ARecordIndex2, AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
var
AYear, AMonth1, AMonth2, ADay: Word;
begin
// Applies custom sorting when records are sorted against the "Sale Date" column
if (AItemIndex = DBTableView1SaleDate.Index) and
(VarType(V1) = VarType(V2)) and (VarType(V1) = varDate) then
begin
DecodeDate(V1, AYear, AMonth1, ADay);
DecodeDate(V2, AYear, AMonth2, ADay);
// Groups records with the same month
Compare := AMonth1 - AMonth2;
end
else // Uses the default comparison algorithm in all other cases
Compare := ADataController.DefaultCompare(ARecordIndex1, ARecordIndex2, AItemIndex);
end;