Home  |  Delphi .net Home  |  System.Array  |  Sort Method
Sort  
Method  
Sorts an array or part of an array into value sequence
Array Class
System NameSpace
CF1.  Procedure Sort ( TheArray : System.Array; ) ;
CF2.  Procedure Sort ( TheArray:System.ArrayTheArray : System.Array; StartIndex : Integer; Count : Integer; ) ; Static;
CF3.  Procedure Sort ( KeyArray:System.ArrayKeyArray : System.Array; ValueArray : System.Array; ) ; Static;
CF4.  Procedure Sort ( KeyArray:System.ArrayKeyArray : System.Array; ValueArray : System.Array; StartIndex : Integer; Count : Integer; ) ; Static;
CF5.  Procedure Sort ( TheArray:System.ArrayTheArray : System.Array; Comparer : IComparer; ) ; Static;
CF6.  Procedure Sort ( TheArray:System.ArrayTheArray : System.Array; StartIndex : Integer; Count : Integer; Comparer : IComparer; ) ;
CF7.  Procedure Sort ( KeyArray:System.ArrayKeyArray : System.Array; ValueArray : System.Array; Comparer : IComparer; ) ; Static;
NotCF8.  Procedure Sort ( KeyArray:System.ArrayKeyArray : System.Array; ValueArray : System.Array; StartIndex : Integer; Count : Integer; Comparer : IComparer; ) ; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
This is a very powerful set of methods for sorting .Net arrays into sequence by whatever criteria you choose.
 
Each array element must support the IComparable interface. This interface is invoked for two individual array elements during the sort process.
 
For example, the String structure supports IComparable, whereby two string values are compared according to their character values. By default, this comparison is case insensitive.
 
You should code your own object Comparer for your own classes. See the IComparable reference below for more on this.
 
There are two basic flavours of Sort :
 
1. Basic array sorting
 
The given array is sorted into value sequence.
 
2. Key/value double array sorting
 
This type sorts a KeyArray into sequence, and performs the same sort actions on a corresponding ValueArray. The net effect is that this pair of key/value arrays are sorted into key sequence without losing the key/value array element relationship.
 
For each of these types, the sort may be optionally performed on a subset of the array(s), as specifeid by the StartIndex and Count values.
References
IComparer
Microsoft MSDN Links
System
System.Array
 
 
Sort all elements of a simple array
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

var
  myArray : System.Array;
  i       : Integer;

begin
  // Create a 4 element single dimension array of strings
  myArray := System.Array.CreateInstance(TypeOf(String), 4);

  // Fill the array with values
  myArray.SetValue('CC', 0);
  myArray.SetValue('bb', 1);
  myArray.SetValue('AA', 2);
  myArray.SetValue('BB', 3);

  // Display the array contents
  for i := 0 to myArray.Length-1 do  // Arrays are always 0 based
    Console.WriteLine('myArray[{0}] = {1}',
                       i.ToString, myArray.GetValue(i));

  // Sort using the default comparator
  Console.WriteLine;
  Console.WriteLine('Sorting the array');
  Console.WriteLine;

  System.Array.Sort(myArray);

  // Display the array contents
  for i := 0 to myArray.Length-1 do
    Console.WriteLine('myArray[{0}] = {1}',
                       i.ToString, myArray.GetValue(i));

  Console.ReadLine;
end.
 
end.
Hide full unit code
  myArray[0] = CC
  myArray[1] = bb
  myArray[2] = AA
  myArray[3] = BB
  
  Sorting the array
  
  myArray[0] = AA
  myArray[1] = bb
  myArray[2] = BB
  myArray[3] = CC
Sort key and value arrays into key sequence
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

var
  keyArray, valueArray : System.Array;
  i                    : Integer;

begin
  // Create two 4 element single dimension arrays of strings
  keyArray   := System.Array.CreateInstance(TypeOf(String), 4);
  valueArray := System.Array.CreateInstance(TypeOf(String), 4);

  // Fill the arrays with values
  keyArray.SetValue('Profession',   0);
  keyArray.SetValue('FirstName',    1);
  keyArray.SetValue('LastName',     2);
  keyArray.SetValue('Age',          3);

  valueArray.SetValue('Programmer', 0);
  valueArray.SetValue('Neil',       1);
  valueArray.SetValue('Moffatt',    2);
  valueArray.SetValue('47',         3);

  // Display the array contents
  for i := 0 to keyArray.Length-1 do
    Console.WriteLine('{0} = {1}',
                       keyArray.GetValue(i), valueArray.GetValue(i));

  // Sort using the default comparator
  Console.WriteLine;
  Console.WriteLine('Sorting the arrays by key');
  Console.WriteLine;

  System.Array.Sort(keyArray, valueArray);

  // Display the array contents
  for i := 0 to keyArray.Length-1 do
    Console.WriteLine('{0} = {1}',
                       keyArray.GetValue(i), valueArray.GetValue(i));

  Console.ReadLine;
end.
 
end.
Hide full unit code
  Profession = Programmer
  FirstName = Neil
  LastName = Moffatt
  Age = 47
  
  Sorting the arrays by key
  
  Age = 47
  FirstName = Neil
  LastName = Moffatt
  Profession = Programmer
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author