TreeNodeCustomSort (C++)

From RAD Studio Code Examples
Jump to: navigation, search


This example shows how to use the CustomSort method to order to sort a subtree in case-insensitive alphabetical order (either forward or backward). The callback function CompareFunc below calls the global AnsiStrIComp function to perform the actual comparison. This example requires two buttons and a populated TreeView.


#include <CommCtrl.hpp>

/* This procedure can then be used as a parameter to CustomSort, 
   in order for it to sort the nodes of the tree view. To sort 
   in ascending order, call:
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)
  TTreeNode *node = TreeView1->Selected;
  node->CustomSort(CompareFunc, 0); // Sorts in ascending order.

void __fastcall TForm1::Button2Click(TObject *Sender)
  TTreeNode *node = TreeView1->Selected;
  node->CustomSort(CompareFunc, 1); // Sorts in descending order.