unit sort;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
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 my_comp(List: TStringList; Index1, Index2: Integer): Integer;
var
strlen1,strlen2:longint;
i,j:longint;
str1,str2:string;
begin
strlen1:=length(list.Strings [index1]);
strlen2:=length(list.Strings [index2]);
str1:=list.Strings [index1];
str2:=list.Strings [index2];
i:=1;
j:=1;
while (i<=strlen1) and (j<=strlen2) do
begin
if str1[i]<str2[j] then
begin
my_comp:=-1;
exit;
end
else
if str1[i]>str2[j] then
begin
my_comp:=1;
exit;
end;
inc(i);
inc(j);
end;
if (i>strlen1) and (j>strlen2) then
my_comp:=0;
if (i>strlen1) then
my_comp:=-1;
if (j>strlen2) then
my_comp:=1; end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:longint;
t:tstringlist;
begin memo1.Lines.Clear ;
t:=tstringlist.Create ;
for i:=1 to 100 do
t.Add(inttostr(i));
t.CustomSort(my_comp) ;
memo1.Lines.Assign(t);
t.Free ;
end;end.回调函数不能使用正常
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
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 my_comp(List: TStringList; Index1, Index2: Integer): Integer;
var
strlen1,strlen2:longint;
i,j:longint;
str1,str2:string;
begin
strlen1:=length(list.Strings [index1]);
strlen2:=length(list.Strings [index2]);
str1:=list.Strings [index1];
str2:=list.Strings [index2];
i:=1;
j:=1;
while (i<=strlen1) and (j<=strlen2) do
begin
if str1[i]<str2[j] then
begin
my_comp:=-1;
exit;
end
else
if str1[i]>str2[j] then
begin
my_comp:=1;
exit;
end;
inc(i);
inc(j);
end;
if (i>strlen1) and (j>strlen2) then
my_comp:=0;
if (i>strlen1) then
my_comp:=-1;
if (j>strlen2) then
my_comp:=1; end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:longint;
t:tstringlist;
begin memo1.Lines.Clear ;
t:=tstringlist.Create ;
for i:=1 to 100 do
t.Add(inttostr(i));
t.CustomSort(my_comp) ;
memo1.Lines.Assign(t);
t.Free ;
end;end.回调函数不能使用正常
t.sort := True;
for ....
一个函数尽然没有result
下面是个简单点的函数,你试下,我这边是OK的。 function my_comp(List: TStringList; Index1, Index2: Integer): Integer;
var
value1, value2: Integer;
begin
value1 := StrToInt(list.Strings[index1]);
value2 := StrToInt(list.Strings[index2]);
if value1> value2 then
Result := -1
else if value1< value2 then
Result := 1
else
Result := 0; end;
stringlist应该也是相同的,只是给出比较结果,不要去修改stringlist的数据
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
还是那样,回调函数的问题
测试了下,回调函数在递归调用时是死循环。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
仔细分析下下面这个函数:
SCompare 就是my_comp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TStringList.QuickSort(L, R: Integer; SCompare: TStringListSortCompare);
var
I, J, P: Integer;
begin
repeat
I := L;
J := R;
P := (L + R) shr 1;
repeat
while SCompare(Self, I, P) < 0 do Inc(I);
while SCompare(Self, J, P) > 0 do Dec(J);
if I <= J then
begin
ExchangeItems(I, J);
if P = I then
P := J
else if P = J then
P := I;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then QuickSort(L, J, SCompare);
L := I;
until I >= R;
end;