Home  |  Delphi .net Home  |  System.Collections.ArrayList  |  Sort Method
Sort  
Method  
Sort the elements in the ArrayList into sequence
ArrayList Class
System.Collections NameSpace
CF1.  Procedure Sort ( ) ;
CF2.  Procedure Sort ( Comparer : IComparer ; ) ;
CF3.  Procedure Sort ( StartIndex:IntegerStartIndex : Integer; Count : Integer; Comparer : IComparer; ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The Reverse method reverses the sequence of the elements in the ArrayList. All elements are reversed unless a range is specified via the StartIndex and Count parameters.
 
The Comparer parameter allows you to supply the comparison object used by the Sort method when it compares ArrayList Elements. The Compare method of Comparer is used.
 
Each element must implement the System.Collections.IComparer interface to facilitate the sort process.
Microsoft MSDN Links
System.Collections
System.Collections.ArrayList
 
 
Sorting the whole ArrayList into sequence
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  List : System.Collections.ArrayList;
  i    : Integer;

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

  // Fill it
  List.Add('2.--');
  List.Add('3.---');
  List.Add('0.');
  List.Add('1.-');

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

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

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

  Console.Readline;
end.
Show full unit code
  2.--
  3.---
  0.
  1.-
  
  Sorting the array into sequence :
  
  0.
  1.-
  2.--
  3.---
Sorting using an IComparer class object
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

type
  // Define our data class
  MyData = Class
    private
      daAge : Integer;
    published
      property Age : Integer
          read daAge;

      constructor Create(Age : Integer);
  end;

  // Define our comparer class
  MyComparer = Class(TObject, System.Collections.IComparer)
    published
      function Compare(Object1 : System.Object;
                       Object2 : System.Object) : Integer;
    end;

// Data class constructor
constructor MyData.Create(Age: Integer);
begin
  Inherited Create;

  daAge := Age;
end;

// Compare method of our comparer class
function MyComparer.Compare(Object1 : System.Object;
                            Object2 : System.Object) : Integer;
var
  Data1, Data2 : MyData;

begin
  // Protect our comparison in case of non-integer objects
  try
    // We cast the Object parameters to our Data type
    Data1 := MyData(Object1);
    Data2 := MyData(Object2);

    // And compare : Return <1, 0 or >1 as appropriate
    Result := Data1.Age - Data2.Age;
  except
    Result := 0;
  end;
end;

// Main code
// ----------------------------------------------------------
var
  List     : System.Collections.ArrayList;
  Comparer : MyComparer;
  i        : Integer;

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

  // Fill it
  List.Add(MyData.Create(254));
  List.Add(MyData.Create(2));
  List.Add(MyData.Create(74));
  List.Add(MyData.Create(11));
  List.Add(MyData.Create(243));
  List.Add(MyData.Create(55));

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

  // Create our comparer object
  Comparer := MyComparer.Create;

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

  List.Sort(Comparer);

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

  Console.Readline;
end.
Show full unit code
  254
  2
  74
  11
  243
  55
  
  Sorting the array into sequence :
  
  2
  11
  55
  74
  243
  254
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author