Home  |  Delphi .net Home  |  System.Collections.CaseInsensitiveComparer  |  Compare Method
Compare  
Method  
Compares one Object value with another without case sensitivity
CaseInsensitiveComparer Class
System.Collections NameSpace
CF1.  Function Compare ( ValueA:ObjectValueA : Object; ValueB : Object; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
The Compare method performs a case-insensitive comparison of ValueA with ValueB, returning a number that describes the relation of the values of these two objects.
 
The Compare method is often called by other .Net classes, such as Sort, as in the example.
Microsoft MSDN Links
System.Collections
System.Collections.CaseInsensitiveComparer
 
 
A hidden use of Compare by ArrayList.Sort
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization,
  System.Collections;

var
  List     : System.Collections.ArrayList;
  Comparer : System.Collections.CaseInsensitiveComparer;
  Culture  : System.Globalization.CultureInfo;
  i        : Integer;

begin
  // Create our array list object
  List := ArrayList.Create;

  // Fill it
  List.Add('DEF');
  List.Add('abc');
  List.Add('ABC');

  // Display List contents : note the 0 indexing
  for i := 0 to List.Count-1 do
    Console.WriteLine(List.Item[i].ToString);

  // Use British English for comparison
  Culture := System.Globalization.CultureInfo.Create('en-GB');

  // Create our comparer object
  Comparer := System.Collections.CaseInsensitiveComparer.Create(Culture);

  // Sort the array into sequence
  Console.WriteLine;
  Console.WriteLine('Sorting the array into sequence :');
  Console.WriteLine;

  // The following uses the Compare method
  List.Sort(Comparer);

  // Display List contents again
  for i := 0 to List.Count-1 do
    Console.WriteLine(List.Item[i].ToString);

  Console.Readline;
end.
 
end.
Hide full unit code
  DEF
  abc
  ABC
  
  Sorting the array into sequence :
  
  ABC
  abc
  DEF
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author