TTreeCustomSort (C++)

From RAD Studio Code Examples
Jump to: navigation, search


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.


#include <CommCtrl.hpp>

int CALLBACK CompareFunc(long lParam1, long lParam2, long Reverse)
  TTreeNode *Node1 = reinterpret_cast<TTreeNode *>(lParam1);
  TTreeNode *Node2 = reinterpret_cast<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;

void __fastcall TForm1::Button1Click(TObject *Sender)
  // To sort in ascending order, call the following
  TreeView1->CustomSort(CompareFunc, 0);