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.
|