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
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.
Show full unit code
  DEF
  abc
  ABC
  
  Sorting the array into sequence :
  
  ABC
  abc
  DEF
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author