1. Computing & Technology
Delphi for .NET FCL Examples Reference
Comparer namespace
System.Collections
Declaration
Comparer = class (System.Object, IComparer);
Description
Compares two objects for equivalence, where string comparisons are case-sensitive.
Example
program System_Collections_Comparer;
{$APPTYPE CONSOLE}
{$AUTOBOX ON}

uses
  System.Collections, System.Globalization;

type
  SamplesComparer = class
  public
    class procedure Main; 
  end;

{ SamplesComparer }

class procedure SamplesComparer.Main;
var
  myCompTrad: Comparer;
  myCompIntl: Comparer;
  str2: string;
  str1: string;
begin
  // Creates the strings to compare
  str1 := 'llegar';
  str2 := 'lugar';

  // Uses the DefaultInvariant Comparer.
  Console.WriteLine('Comparing "{0}" and "{1}" ...',
                    str1, 
                    str2);
  Console.WriteLine('   Invariant Comparer: {0}',
                    Comparer.DefaultInvariant.Compare
                      (str1, str2));

  // Uses the Comparer based on the
  //culture "es-ES" (Spanish - Spain, international sort).
  myCompIntl := Comparer.Create(CultureInfo.Create('es-ES', 
                                                   False));
  Console.WriteLine('   International Sort: {0}',
                    myCompIntl.Compare(str1, str2));

  // Uses the Comparer based on the culture
  //identifier 0x040A (Spanish - Spain, traditional sort).
  myCompTrad := Comparer.Create(CultureInfo.Create(1034, 
                                                   False));
  Console.WriteLine('   Traditional Sort  : {0}',
                    myCompTrad.Compare(str1, str2));
end;

//test program
begin
  SamplesComparer.Main;
end.
Example output
Comparing "llegar" and "lugar" ...
   Invariant Comparer: -1
   International Sort: -1
   Traditional Sort  : 1

Looking for Delphi for .NET examples? Look no further, Delphi for .NET FCL Examples Reference is what you are looking for!

Discuss in my forum

©2012 About.com. All rights reserved.

A part of The New York Times Company.