« ■遅いと思ったら・・・。 | トップページ | ■Visual Studio 2008 Beta2 日本語版 »

☆TListのSortの使用方法

TListのSortを使うと簡単に並び替えができます。
本と著者を管理する簡単なクラスを作って試してみます。

unit TListSortSampleClass;

interface

uses
  Windows, SysUtils, Classes;

type
  TMyBook = class(TObject)
  private
    FBookName: String;
    FAuthor: String;
  public
    constructor Create(BookName, Author: String);
  published
    property BookName: String read FBookName write FBookName;
    property Author: String read FAuthor write FAuthor;
  end;

  TMyBookList = class(TObject)
  private
    FList: TList;                                       { リスト }
    function GetData(Index: Integer): TMyBook;          { 読み込み }
    procedure SetData(Index: Integer; MyBook: TMyBook); { 書き込み }
    function GetCount: Integer;                         { リストのカウント数 }
  protected
    procedure Error;                                    { エラーの表示 }
  public
    constructor Create;                                 { 生成 }
    destructor Destroy; override;                       { 破棄 }
    procedure Clear;                                    { 消去 }
    function Add(MyBook: TMyBook): Integer;             { 追加 }
    procedure Sort;                                     { 並べ替え }
    property Items[Index: Integer]: TMyBook read GetData write SetData; default;
  published
    property Count: Integer read GetCount;              { リストのカウント数 }
  end;

implementation

{ TMyBook }

constructor TMyBook.Create(BookName, Author: String);
begin
  inherited Create;
  FBookName:= BookName;
  FAuthor := Author;
end;

{ TMyBookList }

// 生成
constructor TMyBookList.Create;
begin
  FList := TList.Create;
end;

// 破棄
destructor TMyBookList.Destroy;
begin
  Clear;
  FList.Free;
  inherited Destroy;
end;

// 消去
procedure TMyBookList.Clear;
var
  I: Integer;
begin
  for I := 0 to FList.Count -1 do
    TMyBook(FList[I]).Free;
  FList.Clear;
end;

// 追加
function TMyBookList.Add(MyBook: TMyBook): Integer;
begin
  Result := FList.Add(MyBook);
end;

// エラー処理
procedure TMyBookList.Error;
begin
  raise Exception.Create('インデックスがリストの範囲を超えています');
end;

// リストからの取得
function TMyBookList.GetData(Index: Integer): TMyBook;
begin
  if (Index < 0) or (Index >= FList.Count) then Error;
  Result := TMyBook(FList[Index]);
end;

// リストへの設定
procedure TMyBookList.SetData(Index: Integer; MyBook: TMyBook);
begin
  if (Index < 0) or (Index >= FList.Count) then Error;
  FList[Index] := MyBook;
end;

// リストのカウント数
function TMyBookList.GetCount: Integer;
begin
  Result := FList.Count;
end;

ソート部分のプログラムです。
function ListSortCompare(Item1,Item2: Pointer): Integer; 
var
  S1, S2: String;
begin
  S1 := TMyBook(Item1).BookName;
  S2 := TMyBook(Item2).BookName;
  Result := AnsiCompareText(S1, S2);
end;

procedure TMyBookList.Sort;
begin
  FList.Sort(@ListSortCompare);
end;

end.

上記のリストを利用するサンプルです。
適当にデータを入力して、試してみて下さい。
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;  // 本の名前
    Edit2: TEdit;  // 著者
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private 宣言 }
    MyBookList: TMyBookList;
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyBookList:= TMyBookList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  MyBookList.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyBookList.Add(TMyBook.Create(Edit1.Text, Edit2.Text));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I: Integer;
begin
  // 並べ替え前のデータ
  for I := 0 to MyBookList.Count - 1 do
    Memo1.Lines.Add(MyBookList[I].BookName);

  // 並べ替え後のデータ
  MyBookList.Sort;
  for I := 0 to MyBookList.Count - 1 do
    Memo2.Lines.Add(MyBookList[I].BookName);
end;

end.

Memo1には入力順に、Memo2には、並べ替えされたデータが設定されていると思います。
わざわざクイックソートのプログラムを書かなくても簡単に並べ替えができるので便利ですね。

|

« ■遅いと思ったら・・・。 | トップページ | ■Visual Studio 2008 Beta2 日本語版 »