TLIST的SORT排序高手请进。。。(20分)

B

bobo158

Unregistered / Unconfirmed
GUEST, unregistred user!
我发现TLIST的SORT排序有问题,如下:
...
type
Padd = ^add;
add = record
address:string;
sum:integer;
end;
...
var
...
function mycp(tp1,tp2: padd): Integer;
implementation
function mycp(tp1,tp2: padd): Integer;
begin
if (tp1^.sum ) < (tp2^.sum) then
Result := 1 else Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
myadd:padd;
mylist:tlist;
id:byte;
begin
mylist:=tlist.Create ;
new(myadd); //1
myadd^.address :='aaa';
myadd^.sum :=11;
mylist.Add(myadd);
new(myadd); //2
myadd^.address :='ccc';
myadd^.sum :=115;
mylist.Add(myadd);
new(myadd); //3
myadd^.address :='eee';
myadd^.sum :=5;
mylist.Add(myadd);
new(myadd); //4
myadd^.address :='ggg';
myadd^.sum :=10;
mylist.Add(myadd);
//排序前输出
for id:=0 to (MyList.Count - 1) do
begin
memo1.Lines.Add(padd(mylist.Items[id]).address +' sum: '+inttostr(padd(mylist.Items[id]).sum ));
end;
//排序
mylist.Sort(@mycp);
memo1.Lines.Add('*********');
//排序后输出
for id:=0 to (MyList.Count - 1) do
begin
memo1.Lines.Add(padd(mylist.Items[id]).address +' sum: '+inttostr(padd(mylist.Items[id]).sum ));
end;
for id := 0 to (MyList.Count - 1) do
begin

myadd := MyList.Items[id];
Dispose(myadd);
end;
mylist.Free;
end;
...
输出结果:
//排序前输出
aaa sum: 11
ccc sum: 115
eee sum: 5
ggg sum: 10
*********
//排序后输出
ccc sum: 115
ggg sum: 10
eee sum: 5
aaa sum: 11
排序后,11怎么在10,5的后面?
请高手帮忙解答。
 

function mycp(tp1,tp2: padd): Integer;
begin
if (tp1^.sum ) < (tp2^.sum) then
Result := 0 else Result := 1;
end;
 
Delphi在线帮助说了:
> 0 (positive) Item1 is less than Item2
0 Item1 is equal to Item2
< 0 (negative) Item1 is greater than Item2
所以你的代码有问题
if (tp1^.sum ) < (tp2^.sum) then
Result := 1 else Result := 0;
以下代码我调试过了
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
PMyRecord = ^TMyRecord;
TMyRecord = packed record
Name: string[10];
Num: Integer;
end;

TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function MySort(Item1, Item2: Pointer): Integer;
begin
Result := -1;
if PMyRecord(Item1)^.Num < PMyRecord(Item2)^.Num then
Result := 1
else
if PMyRecord(Item1)^.Num = PMyRecord(Item2)^.Num then
Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
MyList: TList;
i: Integer;
P: PMyRecord;
begin
MyList := TList.Create;
try
New(P);
P^.Name := 'aaa';
P^.Num := 11;
MyList.Add(P);

New(P);
P^.Name := 'ccc';
P^.Num := 115;
MyList.Add(P);

New(P);
P^.Name := 'eee';
P^.Num := 5;
MyList.Add(P);

New(P);
P^.Name := 'ggg';
P^.Num := 10;
MyList.Add(P);

for i := 0 to MyList.Count - 1 do
Memo1.Lines.Add(Format('Name:%s, Num:%d', [PMyRecord(MyList.Items)^.Name, PMyRecord(MyList.Items)^.Num]));
Memo1.Lines.Add('=====================================');
MyList.Sort(@MySort);
for i := 0 to MyList.Count - 1 do
Memo1.Lines.Add(Format('Name:%s, Num:%d', [PMyRecord(MyList.Items)^.Name, PMyRecord(MyList.Items)^.Num]));
finally
for i := MyList.Count - 1 downto 0 do
DisPose(PMyRecord(MyList.Items));
end;

end;

end.
 
to 影 子 兄:
还有问题啊,如把数值11改成10,10改成11后在排序,结果为:
aaa sum: 10
ccc sum: 115
eee sum: 5
ggg sum: 11
*********
eee sum: 5
ggg sum: 11
aaa sum: 10
ccc sum: 115
10怎么在11之后呢?
 
看了看帮助,我错了。更改如下:[:)]

function mycp(tp1,tp2: padd): Integer;
begin
if (tp1^.sum ) > (tp2^.sum) then
Result := 1
else if (tp1^.sum ) = (tp2^.sum) then
Result := 0
else
Result := -1;
end;
 
感谢 影 子,pcexplorer的热心帮助,分数少,不成敬意,望笑纳。
 
顶部