RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomListView.CustomSort Method

Sorts the items in the list using the specified ordering function.

Pascal
function CustomSort(SortProc: TLVCompare; lParam: Longint): Boolean;
C++
__fastcall Boolean CustomSort(TLVCompare SortProc, Longint lParam);

Call CustomSort to sort the items in the list using the ordering function defined by the SortProc parameter. The SortProc parameter specifies an ordering function that compares the list items passed as lParam1 and lParam2. The ordering function returns an integer that indicates whether lParam1 is the same as lParam2 (return value is 0), lParam1 is greater than lParam2 (return value is greater than 0), or lParam1 is less than lParam2 (return value is less than 0). The lParam parameter of CustomSort is an optional value that is passed as the third parameter to the ordering function. 

If the SortProc parameter is nil (Delphi) or NULL (C++), CustomSort compares the list items by generating an OnCompare event. This allows the OnCompare event handler to provide different ordering criteria (such as ascending vs. descending order), based on the value of the lParam parameter. 

If the ordering function is not provided and there is no OnCompare event handler, CustomSort sorts items alphabetically by their Caption values. 

CustomSort returns true if the list is successfully sorted.

Warning: CustomSort does not work if the application is running in virtual mode.
 

C++ Examples: 

 

/*
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);
}

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

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  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";
}

 

Delphi Examples: 

{
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.
}
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
begin
  Result := -CompareText(Item1.Caption,Item2.Caption);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  ListView1.CustomSort(@CustomSortProc, 0);
end;

procedure TForm1.FormCreate(Sender: TObject);
var ListItem : TListItem;
begin
  ListView1.ViewStyle := vsList;
  ListItem := ListView1.Items.Add;
  ListItem.Caption := 'Apples';
  ListItem := ListView1.Items.Add;
  ListItem.Caption := 'Oranges';
  ListItem := ListView1.Items.Add;
  ListItem.Caption := 'Pears';
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!