RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.CompareText Function

Compares two strings by ordinal value without case sensitivity.

Pascal
function CompareText(const S1: string; const S2: string): Integer; overload;
function CompareText(const S1: string; const S2: string; LocaleOptions: TLocaleOptions): Integer; overload;
C++
int CompareText(const AnsiString S1, const AnsiString S2);
int CompareText(const AnsiString S1, const AnsiString S2, TLocaleOptions LocaleOptions);

SysUtils

CompareText compares S1 and S2 and returns 0 if they are equal. If S1 is greater than S2, CompareText returns an integer greater than 0. If S1 is less than S2, CompareText returns an integer less than 0. CompareText is not case sensitive and is not affected by the current locale.  

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(((TListItem *)Item1)->Caption, 
                      ((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";
}
/*
This example shows how to use the OnColumnClick and
OnCompare events of a list view to let users sort the
columns in a report-style list view by clicking on the
column headers. This requires a global variable to keep
track of the column that was clicked.  The OnColumnClick
event handler sets the global variable to indicate the
column to sort and calls AlphaSort:
*/ 
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TListItem *Item;
  TListColumn *Column;
  const char imagenames[3][20] = {
    "C++ Image", "Borland Image", "Delphi Image"};
  const char Col2Array[3][40] = {
    "Documentation for the C++ icon.", 
    "Borland icon.", "Delphi icon."
    };
  // Create a ListView item for each image in the ImageList
  ListView1->SmallImages = ImageList1;
  ListView1->LargeImages = ImageList1;
  for (int i = 0; i < ImageList1->Count; i++)
  {
    Item = ListView1->Items->Add();
    Item->Caption = imagenames[i];
    Item->ImageIndex = i;
    Item->SubItems->Add(Col2Array[i]);
  }
  // Create two columns to show during viewing as vsReport
  Column = ListView1->Columns->Add();
  Column->Caption = "Column 1";
  Column->Width = 200;
  Column = ListView1->Columns->Add();
  Column->Caption = "Column 2";
  Column->Width = 200;
  ListView1->ViewStyle = vsReport;
}

int ColumnToSort = 0;

/*
The OnColumnClick event handler sets the global variable to
indicate the column to sort and calls AlphaSort:
*/

void __fastcall TForm1::ListView1ColumnClick(TObject *Sender,
      TListColumn *Column)
{
  ColumnToSort = Column->Index;
  ((TCustomListView *)Sender)->AlphaSort();
}

/*
The OnCompare event handler causes the list view to sort on
the selected column:
*/

void __fastcall TForm1::ListView1Compare(TObject *Sender, TListItem *Item1,
      TListItem *Item2, int Data, int &Compare)
{
  if (ColumnToSort == 0)
    Compare = CompareText(Item1->Caption,Item2->Caption);
  else
  {
    int ix = ColumnToSort - 1;
    Compare = 
      CompareText(Item1->SubItems->Strings[ix], Item2->SubItems->Strings[ix]);
  }
}

 

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;
{
This example shows how to use the OnColumnClick and
OnCompare events of a list view to let users sort the
columns in a report-style list view by clicking on the
column headers. This requires a global variable to keep
track of the column that was clicked.  The OnColumnClick
event handler sets the global variable to indicate the
column to sort and calls AlphaSort:
} 
var ColumnToSort: Integer;

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  ListItem: TListItem;
  NewColumn: TListColumn;
const
  imagenames: Array[0..2] of string = ('C++ Image', 'Borland Image', 'Delphi Image');
begin
  Col2Array[0]:= 'Documentation for the C++ icon.';
  Col2Array[1]:= 'Borland icon.';
  Col2Array[2]:= 'Delphi icon.';
  // Create a ListView item for each image in the ImageList
  with ListView1 do
  begin
    SmallImages := ImageList1;
    LargeImages := ImageList1;
    for I := 0 to ImageList1.Count - 1 do
    begin
      ListItem := Items.Add;
      Listitem.Caption := imagenames[I];
      ListItem.ImageIndex := I;
      Listitem.SubItems.Add(Col2Array[I]);
    end;
    // Create two columns to show during viewing as vsReport
    NewColumn := Columns.Add;
    NewColumn.Caption := 'Column 1';
    NewColumn.Width := 200;
    NewColumn := Columns.Add;
    NewColumn.Caption := 'Column 2';
    NewColumn.Width := 200;
    ListView1.ViewStyle := vsReport;
  end;
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
begin
  ColumnToSort := Column.Index;
  (Sender as TCustomListView).AlphaSort;
end;

{
The OnCompare event handler causes the list view to sort on
the selected column:
}
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
var
  ix: Integer;
begin
  if ColumnToSort = 0 then
    Compare := CompareText(Item1.Caption,Item2.Caption)
  else begin
   ix := ColumnToSort - 1;
   Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
  end;
end;
{
Note:   This OnCompare event handler uses the global
CompareText function. An application may want to use
AnsiCompareText, CompareStr, or AnsiCompareStr instead,
depending on whether the comparison should be case-sensitive
and whether the locale should be considered.
}

 

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