RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomTreeView.CustomSort Method

Sorts the nodes in the tree view into a customized sort order.

Pascal
function CustomSort(SortProc: TTVCompare; Data: Longint; ARecurse: Boolean = True): Boolean;
C++
__fastcall Boolean CustomSort(TTVCompare SortProc, Longint Data, Boolean ARecurse = True);

CustomSort triggers node sorting or resorting, using a comparison routine indicated by the SortProc parameter. The Data parameter is passed to the comparison routine. The optional ARecurse parameter (default true) specifies that sorting should recursively descend the node tree and sort each subtree in turn. 

If SortProc is nil (Delphi) or NULL (C++), a default comparison routine is used. The default routine uses the OnCompare event handler, if defined. If the OnCompare event handler is not defined, the default routine uses a simple case-sensitive compare of node captions. 

In the comparison routine, the lParam1 and lParam2 parameters refer to two nodes when cast to TTreeNode. The lParamSort parameter is the value previously passed to the Data parameter of CustomSort. The return value of the comparison routine indicates the relative sort order of IParam1 and IParam2: 

Return ValueMeaning 

< 0IParam1 comes before IParam2 

0IParam1 and IParam2 are equivalent 

> 0IParam2 comes before IParam1 

Calling CustomSort has the same effect as calling the same method for the Items property, except that in TTreeNodes, CustomSort is not recursive by default. 

To sort a subtree, call the CustomSort method of the Items property.  

C++ Examples: 

 

/*
This example shows how to use the CustomSort method to order
a tree view in case-insensitive alphabetical order (either
forward or backward). The application must provide a
callback function such as CompareFunc below, which calls the
global AnsiStrIComp function to perform the actual
comparison.  This example requires a button and a populated
TreeView.
*/
int CALLBACK CompareFunc(long lParam1, long lParam2, long Reverse)
{
  TTreeNode *Node1 = ((TTreeNode *)lParam1);
  TTreeNode *Node2 = ((TTreeNode *)lParam2);
  if ((Node1 == NULL) || (Node2 == NULL)) return 0;
  int GT = AnsiStrIComp(Node1->Text.c_str(), Node2->Text.c_str());
  if (Reverse)
    return -GT;
  return GT;
}

/*
This procedure can then be used as a parameter to CustomSort
to sort the nodes of the tree view.  To sort in ascending
order, call:
*/

#include <CommCtrl.hpp>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TreeView1->CustomSort(CompareFunc, 0);
}

 

Delphi Examples: 

{
This example shows how to use the CustomSort method to order
a tree view in case-insensitive alphabetical order (either
forward or backward). The application must provide a
callback function such as CompareFunc below, which calls the
global AnsiStrIComp function to perform the actual
comparison.  This example requires a button and a populated
TreeView.
}
function CustomSortProc(Node1, Node2: TTreeNode; Data: Integer): Integer; stdcall;
begin
  Result := -AnsiStrIComp(PChar(Node1.Text), PChar(Node2.Text));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TreeView1.CustomSort(@CustomSortProc, 0);
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!