Home  |  Delphi .net Home  |  System.Collections.Comparer  |  Compare Method
  
Compare  
Method  
Compares one Object value with another
Comparer Class
System.Collections NameSpace
CF1.  Function Compare ( ValueA : Object; ValueB : Object ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
The Compare method performs a case-sensitive 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.Comparer
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
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.Comparer;
  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.Comparer.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.
   DEF
   abc
   ABC
  
   Sorting the array into sequence :
  
   abc
   ABC
   DEF
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author