我在 Delphi 中创建了一个动态矩阵:
AMatrix : Array of Array of Double;
假设我以这种方式初始化它。
SetLength(Amatrix,1000,10);
并使用一些值填充此矩阵。现在,我想根据存储在第二维特定位置(从 0 到 9)中的特定值对第一维上的 1000 个项目进行排序。
有没有办法创建一个TComparer,可以直接应用在Amatrix上,而不需要创建其他数据结构(TList或TArray)?
最佳答案
Is there a way to create a
TComparer<T>
that can be applied directly on [a variable of typearray of array of Double
] without the need to create other data structures (TList or TArray)?
让我们尝试一下,但为了简单起见,我们将使用整数:
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.
很遗憾,这不会编译,因为 array of Integer
不是可以用作 T
的有效类型.注意这就像你不能使用 array of Integer
作为函数的返回类型。解决方法也是一样的:创建一个定义为 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.
但在现代版本的 Delphi 中,您不需要创建自己的类型(事实上,这是一个坏主意,因为不同的此类类型不兼容)。相反,只需使用 TArray<Integer>
这确实被定义为 array of Integer
-- 这是一个动态的整数数组,就像你的 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.
如果你真的无法改变 A
的定义,您可以使用强制转换:
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.
最后,我还应该指出一个显而易见的事实:可以不使用 TComparer<T>
对数据进行排序。 . (实际上,在 Delphi 2009 中引入泛型之前,您被迫这样做。)
关于delphi - 在Delphi中对Double数组的动态数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64488155/