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.
使用此方法排序时运行程序后报访问地址错误的提示:请问这个排序函数有什么问韪吗?

解决方案 »

  1.   

    Lan:    Char; 
    既然是Char怎么能随便强制转为PChar呢?
      

  2.   

      LanInfo = record 
        SAddr:    WORD;  
          Lan:    PChar; 
      end; 
      

  3.   

    结构里面的必须要定义成Char类型,不能更改
      

  4.   

    result := AnsiStrComp(PChar(string(P_LanInfo(Item1).Lan)), PChar(string(P_LanInfo(Item2).Lan)));
    试试这样看可不可以?
      

  5.   

    function CompareNames(Item1, Item2: Pointer): Integer; 
    begin 
    Result := 0;
    if P_LanInfo(Item1).Lan <> P_LanInfo(Item2).Lan then
    Dec(Result);
    end; 
      

  6.   

    //ListView排序的回调函数,默认的是快速排序法,也可以自己在这里做算法
    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);
      

  7.   

    可以按照ACSII码来排序,代码:function CompareNames(Item1, Item2: Pointer): Integer; 
    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; 
      

  8.   

    result := ord(P_LanInfo(Item1).Lan) - ord(P_LanInfo(Item2).Lan);
      

  9.   

    还有我不明白你为什么要用AnsiStrComp,CompareStr不是更方便吗。result := CompareStr(P_LanInfo(Item1).Lan, P_LanInfo(Item2).Lan);
      

  10.   

    楼上兄弟,因为CompareStr和AnsiStrComp的内存分配不同。
    result := ord(P_LanInfo(Item1).Lan) - ord(P_LanInfo(Item2).Lan);加上指针就是正确的,谢谢!结贴了。谢谢回贴的各位朋友!