unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Math;type
P_LanInfo = ^LanInfo;
LanInfo = record
SAddr: WORD;
Lan: Char;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function CompareNames(Item1, Item2: Pointer): Integer;
begin
result := AnsiStrComp(PChar(P_LanInfo(Item1).Lan), PChar(P_LanInfo(Item2).Lan));
end;procedure TForm1.Button1Click(Sender: TObject);
var
list: Tlist;
PLanInfo: P_LanInfo;
begin
list := Tlist.create;
New(PLanInfo);
PLanInfo.SAddr:= 1;
PLanInfo.Lan := #$03;
list.Add(PLanInfo); New(PLanInfo);
PLanInfo.SAddr:= 2;
PLanInfo.Lan := #$02;
list.Add(PLanInfo); list.Sort(@CompareNames);
end;end.
使用此方法排序时运行程序后报访问地址错误的提示:请问这个排序函数有什么问韪吗?
既然是Char怎么能随便强制转为PChar呢?
SAddr: WORD;
Lan: PChar;
end;
试试这样看可不可以?
begin
Result := 0;
if P_LanInfo(Item1).Lan <> P_LanInfo(Item2).Lan then
Dec(Result);
end;
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var txt1,txt2 : string;
begin
if ParamSort <> 0 then
begin
txt1 := Item1.SubItems.Strings[ParamSort - 1];
txt2 := Item2.SubItems.Strings[ParamSort - 1]; if m_bSort then
begin
Result := CompareText(txt1,txt2);
end
else
begin
Result := -CompareText(txt1,txt2);
end;
end
else
begin
if m_bSort then
begin
Result := CompareText(Item1.Caption,Item2.Caption);
end
else
begin
Result := -CompareText(Item1.Caption,Item2.Caption);
end;
end;
end;
在事件或函数里,写如下语句,然后调用回调函数
ListView.CustomSort(@CustomSortProc,Column.Index);
begin
if Ord(P_LanInfo(Item1).Lan) > Ord(P_LanInfo(Item2).Lan) then
Result := 1
else if Ord(P_LanInfo(Item1).Lan) < Ord(P_LanInfo(Item2).Lan) then
Result := -1
else
Result := 0;
end;
result := ord(P_LanInfo(Item1).Lan) - ord(P_LanInfo(Item2).Lan);加上指针就是正确的,谢谢!结贴了。谢谢回贴的各位朋友!