Skip to main content

TcxCustomTreeView.OnCompare Event

Fires when the sorting process compares two nodes.

Declaration

property OnCompare: TTVCompareEvent read; write;

Remarks

You can force node sorting by calling the AlphaSort method of the control. You can also force the control to resort the tree automatically each time the Text or Data property of a node is changed. Set the SortType property to the appropriate value for this purpose.

By default, the control sorts nodes alphabetically by their captions. However, if a handler for the OnCompare event is written, the control uses the handler to compare nodes. Thus, you may implement any required sorting logic.

Sender specifies the TcxCustomInnerTreeView.

Node1 and Node2 identify the nodes to be compared. Use their properties to determine which of them should reside first within the sorted list. The comparison result must to specified using the Compare parameter. The table below explains what values must be assigned to it.

Value Meaning
Negative Node1 is less than Node2. This means that the node specified by the Node1 parameter will reside above the node specified by the Node2 parameter.
0 Node1 is equal to Node2.
Positive Node1 is greater than Node2. This means that the node specified by the Node1 parameter will reside below the node specified by the Node2 parameter.

The sample code below handles the OnCompare event to custom sort nodes. Nodes are sorted by captions in alphabetical order but nodes that have children are placed to the top of the list.

procedure TForm1.cxTreeView1Compare(Sender: TObject; Node1,
  Node2: TTreeNode; Data: Integer; var Compare: Integer);
begin
  if Node1.HasChildren = Node2.HasChildren then
    Compare := CompareStr(Node1.Text, Node2.Text)
  else
  begin
    if Node1.HasChildren then
      Compare := -1
    else
      Compare := 1;
  end;
end;
See Also