DataLab is a compact statistics package aiming at exploratory data analysis. Please visit the DataLab Web site for more information....


OnSortExchange (Matrix)

Declaration: TMatrix.OnSortExchange: TSortExchgEvent;
{ TSortExchgEvent = procedure (Sender: TObject; ExchgWhat: byte; index1, index2, first, last: longint); }
The OnSortExchange property specifies which event handler should be executed when two columns or rows are exchanged during the sorting process. An OnSortExchange event is triggered when two columns or rows are exchanged during a sorting process. This event can be used to synchronize the sorting of matrix or vector elements with external data structures.

The ExchgWhat parameter specifies whether vector elements ( = 0), matrix columns ( = 1) or matrix rows ( = 2) are exchanged. The parameters index1 and index2 define the indices of the columns (or rows) to be exchanged. The parameters first and last specify the range of rows (or columns) to be affected by the exchange operation.

Example: The following program shows how to use the OnSortExchange event to reflect the sorting order in a structure external to the matrix. The program first copies the data into the TMatrix instance MyMat and fills the integer array Ranks by consecutive numbers from 1 to the number of matrix rows. When sorting the first column, the OnSortExchange event causes the Ranks array to be sorted along the data in column 1, thus leading to an array which contains the ranks of column 1 after the sorting is completed.

program SortRows;

var
  i     : integer;
  MyMat : TMatrix;
  Ranks : TIntArray;

procedure MyMatSortExchange (Sender: TObject; ExchgWhat: byte; index1, index2, first, last: longint);

var
  dummy : integer;

begin
dummy := Ranks[index1-1];
Ranks [index1-1] := Ranks[index2-1];
Ranks [index2-1] := dummy;
end;

begin
MyMat := TMatrix.Create(nil);
MyMat.OnSortExchange := @MyMatSortExchange;
DStore.CopyDataToMatrix (MyMat,0,0,0,0,1,1);
SetLength(Ranks, DStore.NrOfRows);
for i:=1 to DStore.NrOfRows do
  Ranks[i-1] := i;
MyMat.SortRows (1, true, 0, 0, 0, 0);
cout ('ranks: ', ranks);
MyMat.Free;
end.