Is there a way to create a TComparer<T> that can be applied directly on [a variable of type array of array of Double] without the need to create other data structures (TList or TArray)?
Let's try it, but we'll use integers for simplicity:
program FailedAttempt;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils, Math, Generics.Defaults, Generics.Collections;
var
A: array of array of Integer;
begin
A :=
[
[5, 2, 1, 3, 6],
[1, 2, 6, 3, 2],
[1, 6, 7, 8, 3],
[5, 7, 4, 2, 1],
[0, 4, 9, 0, 5],
[4, 1, 8, 9, 6]
];
TArray.Sort<array of Integer>(A,
TComparer<array of Integer>.Construct(
function(const Left, Right: array of Integer): Integer
begin
if Left[2] < Right[2] then
Result := -1
else if Left[2] > Right[2] then
Result := +1
else
Result := 0;
end
)
);
for var i := 0 to High(A) do
begin
Writeln;
for var j := 0 to High(A[i]) do
Write(A[i, j]);
end;
Readln;
end.
Unfortunately, this won't compile, since array of Integer isn't a valid type you can use as T. Notice how this is just like how you cannot use array of Integer as the return type of a function. And the solution is the same, too: Create a type defined as array of Integer.
program Solution1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils, Math, Generics.Defaults, Generics.Collections;
type
TIntArray = array of Integer;
var
A: array of TIntArray;
begin
A :=
[
[5, 2, 1, 3, 6],
[1, 2, 6, 3, 2],
[1, 6, 7, 8, 3],
[5, 7, 4, 2, 1],
[0, 4, 9, 0, 5],
[4, 1, 8, 9, 6]
];
TArray.Sort<TIntArray>(A,
TComparer<TIntArray>.Construct(
function(const Left, Right: TIntArray): Integer
begin
if Left[2] < Right[2] then
Result := -1
else if Left[2] > Right[2] then
Result := +1
else
Result := 0;
end
)
);
for var i := 0 to High(A) do
begin
Writeln;
for var j := 0 to High(A[i]) do
Write(A[i, j]);
end;
Readln;
end.
But in modern versions of Delphi, you don't need to create your own type (and, indeed, that is a bad idea, since different such types are not compatible). Instead, just use TArray<Integer> which is indeed defined as array of Integer -- this is a dynamic array of integers, just like your array of Integer:
program Solution2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils, Math, Generics.Defaults, Generics.Collections;
var
A: array of TArray<Integer>;
begin
A :=
[
[5, 2, 1, 3, 6],
[1, 2, 6, 3, 2],
[1, 6, 7, 8, 3],
[5, 7, 4, 2, 1],
[0, 4, 9, 0, 5],
[4, 1, 8, 9, 6]
];
TArray.Sort<TArray<Integer>>(A,
TComparer<TArray<Integer>>.Construct(
function(const Left, Right: TArray<Integer>): Integer
begin
if Left[2] < Right[2] then
Result := -1
else if Left[2] > Right[2] then
Result := +1
else
Result := 0;
end
)
);
for var i := 0 to High(A) do
begin
Writeln;
for var j := 0 to High(A[i]) do
Write(A[i, j]);
end;
Readln;
end.
If you really cannot change the definition of A, you can use casting:
program Solution3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils, Math, Generics.Defaults, Generics.Collections;
var
A: array of array of Integer;
begin
A :=
[
[5, 2, 1, 3, 6],
[1, 2, 6, 3, 2],
[1, 6, 7, 8, 3],
[5, 7, 4, 2, 1],
[0, 4, 9, 0, 5],
[4, 1, 8, 9, 6]
];
TArray.Sort<TArray<Integer>>(TArray<TArray<Integer>>(A),
TComparer<TArray<Integer>>.Construct(
function(const Left, Right: TArray<Integer>): Integer
begin
if Left[2] < Right[2] then
Result := -1
else if Left[2] > Right[2] then
Result := +1
else
Result := 0;
end
)
);
for var i := 0 to High(A) do
begin
Writeln;
for var j := 0 to High(A[i]) do
Write(A[i, j]);
end;
Readln;
end.
Finally, I should also point out the obvious: it is possible to sort your data without using a TComparer<T>. (Indeed, you were forced to do so before generics were introduced in Delphi 2009.)