Let's say you have a type,

1
2
3
4
5
6
type
  TMySecretData = record
     Timestamp: TDateTime;
     SecretInfo: string; // or some unknown type....
     class function Create(ATimestamp: TDateTime; const ASecretInfo: string): TMySecretData; static;
  end;

and you have a declaration of a list of TMySecretData:

1
2
var
  List: TList<TMySecretData>;

you have already populated this list, and now you want to sort the list.

All you have to do is create a delegated comparer with an anonymous compare function that compares one of the fields in an instance of your type with another instance:

1
2
3
4
5
6
7
8
List.Sort(TDelegatedComparer<TMySecretData>.Create(function (const Left, Right: TMySecretData): Integer begin
// Perform the comparison here
  if Left.SecretInfo < Right.SecretInfo then
     Result := -1 else
  if Left.SecretInfo > Right.SecretInfo then
     Result := 1 else
     Result := 0;
end));

TDelegatedComparer will then create an instance of the comparer: IComparer<TMySecretData> and provide that to the Sort method. One of the mistakes that can be easily overlooked is missing the "const" keyword.

Here's the complete compilable example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
uses
  System.Generics.Collections,
  System.Generics.Defaults,
  System.SysUtils,
  System.DateUtils;
 
type
  TMySecretData = record
     Timestamp: TDateTime;
     SecretInfo: string; // or some unknown type....
     class function Create(const ATimestamp: TDateTime; const ASecretInfo: string): TMySecretData; static;
  end;
 
{ TMySecretData }
 
class function TMySecretData.Create(const ATimestamp: TDateTime;
  const ASecretInfo: string): TMySecretData;
begin
  Result.Timestamp := ATimestamp;
  Result.SecretInfo := ASecretInfo;
end;
 
var
  List: TList<TMySecretData>;
  Item: TMySecretData;
begin
  List := TList<TMySecretData>.Create;
 
  List.Add(TMySecretData.Create(Now, 'Secret 2'));
  List.Add(TMySecretData.Create(Now, 'Secret 4'));
  List.Add(TMySecretData.Create(Now, 'Secret 1'));
 
  for Item in List do         // Before the sort
    begin
      WriteLn(Item.SecretInfo);
    end;
 
  List.Sort(TDelegatedComparer<TMySecretData>.Create(function (const Left, Right: TMySecretData): Integer begin
    if Left.SecretInfo < Right.SecretInfo then
       Result := -1 else
    if Left.SecretInfo > Right.SecretInfo then
       Result := 1 else
       Result := 0;
  end));
 
  for Item in List do         // After the sort
    begin
      WriteLn(Item.SecretInfo);
    end;
  List.Free;
end.