Sort an array (array) of double based on the chosen column

0

I have a multidimensional array in Delphi with 3 rows and 2 columns, I wanted a way to sort it based on a column, follow the example:

procedure;
var
Matriz: array of array of Double;
begin

  SetLength(Matriz,3,2);

  Matriz[1,1]:= 1;
  Matriz[2,1]:= 2;
  Matriz[3,1]:= 3;
  Matriz[1,2]:= 3;
  Matriz[2,2]:= 2;
  Matriz[3,2]:= 1;

end;

My output would be if I sort it by the second column in descending order:

  Matriz[1,1]:= 3;
  Matriz[2,1]:= 2;
  Matriz[3,1]:= 1;
  Matriz[1,2]:= 1;
  Matriz[2,2]:= 2;
  Matriz[3,2]:= 3;

I thought that Delphi had something like sort (Matrix, 2), but in terms of working with array I always have to do a routine, is there any way to make it simpler (nor did I manually) p>     

asked by anonymous 06.03.2015 / 19:12

1 answer

1

Using the QuickSort sort algorithm, I just modified to accept the array instead of the array:

First define the type:

TMatrixType = array of array of Double;

The Algorithm receives 4 parameters, the matrix, the minimum index of the row, the maximum index of the row and the column you want to sort:

procedure QuickSortDoubleMatrix(var Matrix: TMatrixType; iLo, iHi: Integer; Column: Integer);
var
  Lo, Hi: Integer;
  T, Pivot: Double;
begin
  Lo := iLo;
  Hi := iHi;
  Pivot := Matrix[(Lo + Hi) div 2][Column];
  repeat
    while Matrix[Lo][Column] < Pivot do Inc(Lo) ;
    while Matrix[Hi][Column] > Pivot do Dec(Hi) ;
    if Lo <= Hi then
    begin
      T := Matrix[Lo][Column];
      Matrix[Lo][Column] := Matrix[Hi][Column];
      Matrix[Hi][Column] := T;
      Inc(Lo);
      Dec(Hi);
    end;
  until Lo > Hi;
  if Hi > iLo then
    QuickSortDoubleMatrix(Matrix, iLo, Hi, Column);
  if Lo < iHi then
    QuickSortDoubleMatrix(Matrix, Lo, iHi, Column);
end;

Create a console application:

var
  Matriz: TMatrixType;
  Linhas, Colunas, I,J: Integer;

begin
  SetLength(Matriz,6,2);

  Matriz[0,0]:= 1;
  Matriz[1,0]:= 2;
  Matriz[2,0]:= 3;
  Matriz[0,1]:= 3;
  Matriz[1,1]:= 2;
  Matriz[2,1]:= 1;

  Matriz[3,0]:= 4;
  Matriz[4,0]:= 5;
  Matriz[5,0]:= 6;
  Matriz[3,1]:= 6;
  Matriz[4,1]:= 5;
  Matriz[5,1]:= 4;

  Linhas:= 5;
  Colunas:= 1;

  for J:= 0 to Colunas do
    for I:= 0 to Linhas do
      Writeln(format('Matriz[%d, %d] = %.2f', [I+1,J+1, Matriz[I,J]]));

  QuickSortDoubleMatrix(Matriz, 0, Linhas, Colunas);

  Writeln('');
  Writeln('');
  for J:= 0 to Colunas do
    for I:= 0 to Linhas do
      Writeln(format('Matriz[%d, %d] = %.2f', [I+1,J+1, Matriz[I,J]]));
  Readln;
end.

Output:

Matriz[1, 1] = 1,00
Matriz[2, 1] = 2,00
Matriz[3, 1] = 3,00
Matriz[4, 1] = 4,00
Matriz[5, 1] = 5,00
Matriz[6, 1] = 6,00
Matriz[1, 2] = 3,00
Matriz[2, 2] = 2,00
Matriz[3, 2] = 1,00
Matriz[4, 2] = 6,00
Matriz[5, 2] = 5,00
Matriz[6, 2] = 4,00


Matriz[1, 1] = 1,00
Matriz[2, 1] = 2,00
Matriz[3, 1] = 3,00
Matriz[4, 1] = 4,00
Matriz[5, 1] = 5,00
Matriz[6, 1] = 6,00
Matriz[1, 2] = 1,00
Matriz[2, 2] = 2,00
Matriz[3, 2] = 3,00
Matriz[4, 2] = 4,00
Matriz[5, 2] = 5,00
Matriz[6, 2] = 6,00
    
26.03.2015 / 15:37