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 |
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.
| Show 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 | 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.
| Show full unit code | Profession = Programmer
FirstName = Neil
LastName = Moffatt
Age = 47
Sorting the arrays by key
Age = 47
FirstName = Neil
LastName = Moffatt
Profession = Programmer
|
|
|
|