TList的Sort使用方法

来源:互联网 发布:网络综合布线施工合同 编辑:程序博客网 时间:2024/06/10 18:25

有网友提问,做一Demo,以作记录。

unit Unit1;

interface

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

type
  P_MissInfo = ^MissInfo;
  MissInfo = record
    Missqty: integer;
    MissRate: Double;
  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 := Integer(CompareValue(P_MissInfo(Item1).MissRate, P_MissInfo(Item2).MissRate));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  list: Tlist;
  PMissInfo: P_MissInfo;
begin
  list := Tlist.create;
  New(PMissInfo);
  PMissInfo.Missqty:= 10;
  PMissInfo.MissRate:= 12.56;
  list.Add(PMissInfo);

  New(PMissInfo);
  PMissInfo.Missqty:= 12;
  PMissInfo.MissRate:= 12.8;
  list.Add(PMissInfo);

  New(PMissInfo);
  PMissInfo.Missqty:= 9;
  PMissInfo.MissRate:= 11.56;
  list.Add(PMissInfo);

  list.Sort(@CompareNames);
  Showmessage(IntToStr(list.Count));
  showmessage(FloatToStr(P_MissInfo(list.Items[0]).MissRate));
  showmessage(FloatToStr(P_MissInfo(list.Items[1]).MissRate));
  showmessage(FloatToStr(P_MissInfo(list.Items[2]).MissRate));
end;

end.

0 0
原创粉丝点击