5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Delphi] [小ネタ] Generic 版 TList の Sort は複雑だと思ってない?

Last updated at Posted at 2021-04-27

実はたったこれだけ!

// uses に System.Generics.Defaults を追加

FList.Sort( // FList は System.Generic.Collections.TList<T> 型
  TComparer<T>.Construct(
    function(const L, R: T): Integer
    begin
      Result := // マイナス値, 0, プラス値、を返す処理
    end
  )
);

たとえば Integer だとこう

FList.Sort( 
  TComparer<Integer>.Construct(
    function(const L, R: Integer): Integer
    begin
      Result := L - R;
    end
  )
);

なんで、こんな事を書いたかというとわざわざ Comparer を定義してインスタンスを作って渡して、というすごい面倒くさい事を書いてる記事があったので。
Generic 版の TList もこんなに簡単だよ。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?