Sort a double array (array) based on the chosen column

Asked

Viewed 1,191 times

0

I have a multidimensional matrix in Delphi with 3 lines and 2 columns, I wanted a way to sort (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 exit would be this case I order her 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 Delphi had something like Sort(Matrix,2), but in terms of working with matrix I always have to do a routine, is there any way to make it simpler (not even manually)? A numerical library?

  • Which version of Delphi?

  • @Caputo XE5, are there functions for this? Or are you thinking of a generic Tlist<T> ?

  • What sort algorithm do you need? can be quicksort?

1 answer

1


Using the Quicksort sorting algorithm, I modified only to accept the array instead of the array:

First set the type:

TMatrixType = array of array of Double;

The Algorithm receives 4 parameters, the matrix, the minimum row index, the maximum row index 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
  • guy thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.