personinfo = record
 name:string;
 telephone:string;
 add:string;
end;personlist:Tlisttmp:^personinfo ;
new(tmp);
tmp.name:='nm1';
tmp.telphone:='tel1';
tmp.add:='add1';
personlist.add(tmp)

解决方案 »

  1.   

    Delphi 帮助中的:
    Performs a QuickSort on the list based on the comparison function Compare. 
    ------------------------------------------
    type TListSortCompare = function (Item1, Item2: Pointer): Integer;
    procedure Sort(Compare: TListSortCompare);DescriptionCall Sort to sort the items in the Items array. Compare is a comparison function that indicates how the items are to be ordered. Compare returns < 0 if Item1 is less and Item2, 0 if they are equal and > 0 if Item1 is greater than Item2.
    ------------------------------------------
    The following code sorts the objects in a list in alphabetical order based on their names. It assumes that the list contains only component references.
    The CompareNames function performs the comparisons between objects in the list. The list is sorted when the user clicks a button.function CompareNames(Item1, Item2: Pointer): Integer;
    begin
      Result := CompareText((Item1 as TComponent).Name, (Item2 as TComponent).Name);
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      List1.Sort(@CompareText);
    end;
      

  2.   

    The following code sorts the objects in a list in alphabetical order based on their names. It assumes that the list contains only component references.
    The CompareNames function performs the comparisons between objects in the list. The list is sorted when the user clicks a button.function CompareNames(Item1, Item2: Pointer): Integer;
    begin
      Result := CompareText((Item1 as TComponent).Name, (Item2 as TComponent).Name);
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      List1.Sort(@CompareText);
    end;
      

  3.   

    example:The following code sorts the objects in a list in alphabetical order based on their names. It assumes that the list contains only component references.
    The CompareNames function performs the comparisons between objects in the list. The list is sorted when the user clicks a button.function CompareNames(Item1, Item2: Pointer): Integer;
    begin
      Result := CompareText((Item1 as TComponent).Name, (Item2 as TComponent).Name);
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      List1.Sort(@CompareText);
    end;
      

  4.   

    TListSortCompare = function (Item1, Item2: Pointer): Integer;
    // 這是一個函數指針類型...procedure Sort(Compare: TListSortCompare);
    // 傳入一個對應得比較函數得指針,函數滿足上面得定義形式
      

  5.   

    比如我想对姓名排序:
    function CompareNames(Item1, Item2: Pointer): Integer;
    begin
      Result := CompareText((Item1 as TComponent).Name, (Item2 as TComponent).Name);
    end;
    这个函数怎么写?这个帮助我看过了,可惜不会写这样的函数
      

  6.   

    type
      NameRec = record
        Name: string;
      end;
      PNameRec = ^NameRec;function MySortName(pName1, pName2: Pointer): Integer;
    begin
      Result := CompareText((PNameRec(pName1)).Name, (PNameRec(pName2)).Name);
    end;procedure TForm1.Button6Click(Sender: TObject);
    var
      Lt:   TList;
      pRec: PNameRec;
    begin
      Lt := TList.Create;
      GetMem(pRec, SizeOf(NameRec));
      pRec.Name := 'bbb';
      Lt.Add(pRec);
      GetMem(pRec, SizeOf(NameRec));
      pRec.Name := 'aaa';
      Lt.Add(pRec);  ShowMessage((PNameRec(Lt[0])).Name + '##'+ (PNameRec(Lt[1])).Name););
      // here: bbb##aaa  Lt.Sort(@MySortName);  ShowMessage((PNameRec(Lt[0])).Name + '##'+ (PNameRec(Lt[1])).Name);
      // here: aaa##bbb then,the list is sorted...
    end;
      

  7.   

    function MySortName(pName1, pName2: Pointer): Integer;
    begin
      Result := CompareText((PNameRec(pName1)).Name, (PNameRec(pName2)).Name);
    end;
    這個就是相當於一個 回調函數。。也就是說你自己的規則--排序規則。。
    所以要求你傳入對應的排序規則函數的地址進去。。它在裡面就是使用你地這個規則進行排序。。
    呵呵 上面代碼 忘記釋放內存了,剛就想回答你這個問題,沒注意其它得了。。要記得呀。。
      

  8.   

    function CompareNames(Item1, Item2: Pointer): Integer;
    begin
      Result := CompareText((Item1 as TComponent).Name, (Item2 as TComponent).Name);
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      List1.Sort(@CompareNames);
    end;
      

  9.   

    Result := CompareText((PNameRec(pName1)).Name, (PNameRec(pName2)).Name);对头,我说我怎么无法传递我的参数。可能是我的格式不对。我费大劲也没搞清这句话。
    多谢大虾beyondtkl(大龙驹<*百善孝为先*>) 
      

  10.   

    呵呵。。
    function CompareNames(Item1, Item2: Pointer): Integer;
    //這裡Item1,Item2都是指針 所以需要 轉換 需要有意義得轉換一般是這樣
    數據類型(指針型)(指針)((PNameRec(pName1)).Name
    ->
    var
     ptmp: PNameRec;ptmp := PNameRec(pName1);
    然後獲取 ptmp.Name...
      

  11.   

    谢谢beyondtkl(大龙驹<*百善孝为先*>) !学到不少东西。