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
|
|
|
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.
| Show full unit code | DEF
abc
ABC
Sorting the array into sequence :
abc
ABC
DEF
|
|
|
|