如何对键入的列表进行排序
我有许多类型化的 TList,我在排序时遇到问题
通常,对于非类型化的 TList,我会有一个函数,例如:
function SortByJob(Item1: Pointer; Item2: Pointer): Integer;
var
p1, p2: JobPointer;
begin
p1 := JobPointer(Item1);
p2 := JobPointer(Item2);
if p1.job > p2.job then
Result := 1
else
if p1.job = p2.job then
Result := 0
else
Result := -1
end;
列表将调用该
JobList.Sort(SortByJob)
函数但是我已经决定在当前的应用程序中我们想要锁定TList 指向某些指针类型,因此在上面的示例中,我们将 JobList 声明为:
JobList: array[0..4] of TList<JobsPointer>;
现在,当我调用时,
JobList[0].Sort(SortByJob)
我收到“参数不足”错误。
有什么想法吗?
我比较过,如果我在无类型的“标准”TList 上使用上面的 Sort 函数,那么它将正确编译......
I have a number of Typed TLists which I am having problems getting to sort
Normally, for an untyped TList, I would have a function such as:
function SortByJob(Item1: Pointer; Item2: Pointer): Integer;
var
p1, p2: JobPointer;
begin
p1 := JobPointer(Item1);
p2 := JobPointer(Item2);
if p1.job > p2.job then
Result := 1
else
if p1.job = p2.job then
Result := 0
else
Result := -1
end;
Which would be called by the list
JobList.Sort(SortByJob)
However I have decided in my current application that we want to lock the TLists to certain pointer types, so in the above example we would have the JobList declared as:
JobList: array[0..4] of TList<JobsPointer>;
Now when I call
JobList[0].Sort(SortByJob)
I get a "not enough parameters" error.
Any ideas?
I have compared that if I use the Sort function above on an untyped "standard" TList then it will compile correctly...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通用列表使用 IComparer 实例进行排序。下面是一个对整数列表进行排序的示例:
A generic list is sorted using a instance of IComparer. Here is an example that sorts a list of integers: