CustomSort (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code orders a list view in reverse alphabetical order on the click of a button. The callback function CustomSortProc calls the global CompareText function and negates its return value.

int __stdcall CustomSortProc(long Item1, long Item2, long ParamSort) {

 return -CompareText((reinterpret_cast<TListItem *>(Item1))->Caption,

(reinterpret_cast<TListItem *>(Item2))->Caption); }

Code

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

__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
  TListItem *ListItem;
  ListView1->ViewStyle = vsList;
  ListItem = ListView1->Items->Add();
  ListItem->Caption = "Apples";
  ListItem = ListView1->Items->Add();
  ListItem->Caption = "Oranges";
  ListItem = ListView1->Items->Add();
  ListItem->Caption = "Pears";
}

Uses

Personal tools
Assistance