【发布时间】:2012-11-06 13:35:01
【问题描述】:
我有点像 Delphi 新手,我不明白如何调用 TList of Records 的 Sort 方法以按升序整数值对记录进行排序。 我有如下记录:
type
TMyRecord = record
str1: string;
str2: string;
intVal: integer;
end;
以及此类记录的通用列表:
TListMyRecord = TList<TMyRecord>;
试图在帮助文件中找到一个代码示例并找到了这个:
MyList.Sort(@CompareNames);
我不能使用,因为它使用类。所以我尝试用一些不同的参数编写自己的比较函数:
function CompareIntVal(i1, i2: TMyRecord): Integer;
begin
Result := i1.intVal - i2.intVal;
end;
但是当我用open.Sort(CompareIntVal); 调用它时,编译器总是抛出一个“参数不足”的错误,这似乎很明显;所以我尽量靠近帮助文件:
function SortKB(Item1, Item2: Pointer): Integer;
begin
Result:=PMyRecord(Item1)^.intVal - PMyRecord(Item2)^.intVal;
end;
PMyRecord 为PMyRecord = ^TMyRecord;
我尝试了不同的调用函数的方法,总是出错...
【问题讨论】: