delphi的链表排序方法

用delohi编程,用到tlist链表的排序,从大富翁www.delphibbs.com哪里找来的,经自己改进。
原来的是

function mycp(tp1,tp2: FMyList): Integer;
begin
if (tp1^.fa1 > tp2^.fa1) then
Result := 1
else if (tp1^.fa1 = tp2^.fa1) then
Result := 0
else
Result := -1;
end;

用这代码后,排序有问题:
输出结果:
//排序前输出
5 11
1 115
5 5
3 10
3 2
*********
//排序后输出
1 115
3 10
3 2
5 11
5 5

为了令第二行也能排序,于是改进如下:
function mycp(tp1,tp2: FMyList): Integer;
begin
if (tp1^.fa1 > tp2^.fa1) then
Result := 1
else if (tp1^.fa1 = tp2^.fa1) then
begin
if (tp1^.fa2 > tp2^.fa2) then
result:=1
else if (tp1^.fa2 = tp2^.fa2) then
Result := 0
else
result:=-1;
end
else
Result := -1;
end;

这次排序后:
1 115
3 2
3 10
5 5
5 11

调用: biaolist.Sort(@mycp);
biaolist是我定义的tlist

参考网址: http://www.delphibbs.com/delphibbs/dispq.asp?lid=1463137

关于无聊人

一个无聊人而已
此条目发表在delphi分类目录,贴了, , 标签。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注