TOrderedDictionary in Delphi 12.2 (en)

With Delphi 12.2, an exciting new class has been introduced: TOrderedDictionary. This class provides a useful addition for cases where an ordered dictionary structure was previously missing.

In this post, we will take a look at how TOrderedDictionary works and how to use a custom IComparer to perform a specific sorting.

Why TOrderedDictionary?

Traditional dictionaries like TDictionary in Delphi provide fast access to key-value pairs, but they do not maintain any specific order. This is also completely analogous to standard references on data structures, where dictionaries are usually defined like sets without order.

TOrderedDictionary, however, is designed to maintain the order of elements according to their insertion order, meaning they have an order. Moreover, TOrderedDictionary allows for explicit sorting of both keys and values. You can therefore reorder the collection at will.

Basics of Using TOrderedDictionary

In the following, I have put together a simple example that illustrates how TOrderedDictionary works. In this case, we initialize a dictionary with some sample pairs, sort by different criteria, and use a custom IComparer to order the keys by English number words. The example was originally inspired by Marco’s example, where Marco left the question open as to how one could logically sort the number words he chose.

program OrderedDictionaryDemo;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Generics.Defaults,
  System.Generics.Collections,
  System.Diagnostics;

const
  // String constant for the number words, can be easily extended
  NUMBER_WORDS =
    '''
     one two three four five
     six seven eight nine ten
    ''';

type
  TNumberWordComparer = class(TInterfacedObject, IComparer<string>)
  private
    FNumberWords: TArray<string>;
    function GetNumberValue(const Word: string): Integer;
  public
    constructor Create;
    // Compares two strings based on their numeric value as defined in NUMBER_WORDS
    function Compare(const Left, Right: string): Integer;
  end;

constructor TNumberWordComparer.Create;
begin
  // Split the multiline string into individual words
  FNumberWords := NUMBER_WORDS.Split([sLineBreak, ' ']);
end;

function TNumberWordComparer.GetNumberValue(const Word: string): Integer;
var
  Index: Integer;
begin
  // Find the word in the array and return the index as its value
  Index := TArray.IndexOf<string>(FNumberWords, LowerCase(Word));
  if Index >= 0 then
    Result := Index
  else
    Result := MaxInt; // Return value for words not found
end;

function TNumberWordComparer.Compare(const Left, Right: string): Integer;
begin
  // Compare the two words and return a negative value if
  // the left word-number is smaller than the right one
  Result := GetNumberValue(Left) - GetNumberValue(Right);
end;

begin
  var ODict := TOrderedDictionary<string, string>.Create;
  ODict.Add('one', 'London');
  ODict.Add('two', 'Berlin');
  ODict.Add('three', 'Rome');
  ODict.Add('four', 'Athens');
  ODict.Add('five', 'Madrid');
  ODict.Add('six', 'Bruxelles');
  ODict.Add('seven', 'Paris');

  // Sorting and displaying the elements
  Writeln('Default order:');
  for var APair in ODict do
    Writeln(APair.Key + ' - ' + APair.Value);

  // Sorting by keys and values
  ODict.SortByKeys;
  Writeln('Sorted by keys:');
  for var APair in ODict do
    Writeln(APair.Key + ' - ' + APair.Value);

  ODict.SortByValues;
  Writeln('Sorted by values:');
  for var APair in ODict do
    Writeln(APair.Key + ' - ' + APair.Value);

  // Custom sorting by number words
  ODict.SortByKeys(TNumberWordComparer.Create);
  Writeln('Sorted by number words:');
  for var APair in ODict do
    Writeln(APair.Key + ' - ' + APair.Value);
end.

Features and Flexibility

In our example, we use SortByKeys and SortByValues to sort elements by keys and values. TOrderedDictionary thus offers us a convenient way to change the sorting order as needed.

Of particular interest is the ability to implement a custom IComparer. In this case, we sort by English number words (onetwothree, etc.) using TNumberWordComparer.

The GetNumberValue method searches an array of number words to find the index of a word. This index is used as the numeric value for sorting. If a word is not found, the function returns MaxInt, placing it at the end of the list. The array can be extended as needed. The use of a multi-line string introduced in Delphi 12 makes initializing the array via the Split function easy to maintain.

Important: The comparer is only used when explicitly sorting, not implicitly when new pairs are inserted. Therefore, the dictionary remains a static data structure.

Conclusion

The new TOrderedDictionary class is an interesting addition to the System.Generics.Collections unit in Delphi 12.2. With its ability to maintain the order of insertions and perform flexible sorting, it offers developers a versatile and practical solution for managing key-value pairs.

TOrderedDictionary in Delphi 12.2

Mit Delphi 12.2 wurde eine spannende neue Klasse eingeführt: TOrderedDictionary. Diese Klasse bringt eine sinnvolle Erweiterung, für Fälle wo man bisher eine geordnete Dictionary-Struktur vermisst hat.

 In diesem Beitrag schauen wir uns an, wie TOrderedDictionary funktioniert und wie man mithilfe eines benutzerdefinierten IComparer eine spezielle Sortierung vornimmt.

Warum TOrderedDictionary?

Traditionelle Dictionaries wie TDictionary in Delphi bieten schnellen Zugriff auf Schlüssel-Wert-Paare, aber sie behalten keine bestimmte Reihenfolge bei. Dies ist auch völlig analog zu Standardwerken über Datenstrukturen, wo Dictionaries üblicherweise wie Mengen ohne Ordnung definiert werden.  

TOrderedDictionary hingegen ist darauf ausgelegt, die Reihenfolge der Elemente nach ihrer Einfügereihenfolge beizubehalten, sie haben also eine Ordnung. Darüber hinaus erlaubt TOrderedDictionary eine explizite Sortierung sowohl der Schlüssel als auch der Werte. Man kann also die Ordnung beliebig umsortieren.

Grundlagen der TOrderedDictionary-Verwendung

Im Folgenden habe ich ein einfaches Beispiel zusammengestellt, das die Funktionsweise von TOrderedDictionaryillustriert. In diesem Fall initialisieren wir ein Dictionary mit einigen Beispielpaaren, sortieren nach verschiedenen Kriterien und verwenden einen benutzerdefinierten IComparer, um die Schlüssel nach englischen Zahlenwörtern zu ordnen. Das Beispiel ist ursprünglich von Marcos Beispiel inspiriert, in dem Marco aber die Frage offen lässt, wie man dann tatsächlich die von ihm gewählten Zahlenworte logisch sortieren kann. 

program OrderedDictionaryDemo;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Generics.Defaults,
  System.Generics.Collections,
  System.Diagnostics;

const
  // Stringkonstante für die Zahlenwörter, kann einfach beliebig erweitert werden 
  NUMBER_WORDS =
    '''
     one two three four five
     six seven eight nine ten
    ''';

type
  TNumberWordComparer = class(TInterfacedObject, IComparer<string>)
  private
    FNumberWords: TArray<string>;
    function GetNumberValue(const Word: string): Integer;
  public
    constructor Create;
    // Vergleicht zwei Zeichenfolgen basierend auf ihrem numerischen Wert, wie in NUMBER_WORDS definiert
    function Compare(const Left, Right: string): Integer;
  end;

constructor TNumberWordComparer.Create;
begin
  // Zerlege den Multiline-String in einzelne Wörter
  FNumberWords := NUMBER_WORDS.Split([sLineBreak, ' ']);
end;

function TNumberWordComparer.GetNumberValue(const Word: string): Integer;
var
  Index: Integer;
begin
  // Suche das Wort im Array und gebe den Index als Wert zurück
  Index := TArray.IndexOf<string>(FNumberWords, LowerCase(Word));
  if Index >= 0 then
    Result := Index
  else
    Result := MaxInt; // Rückgabewert für nicht gefundene Wörter
end;

function TNumberWordComparer.Compare(const Left, Right: string): Integer;
begin
  Vergleiche die beiden Worte und gebe einen negativen Wert zurück, wenn
  die linke Wort-Zahl kleiner als die rechte ist.
  Result := GetNumberValue(Left) - GetNumberValue(Right);
end;

begin
  var ODict := TOrderedDictionary<string, string>.Create;
  ODict.Add('one', 'London');
  ODict.Add('two', 'Berlin');
  ODict.Add('three', 'Rome');
  ODict.Add('four', 'Athens');
  ODict.Add('five', 'Madrid');
  ODict.Add('six', 'Bruxelles');
  ODict.Add('seven', 'Paris');

  // Sortieren und Ausgabe der Elemente
  Writeln('Standardreihenfolge:');
  for var APair in ODict do
    Writeln(APair.Key + ' - ' + APair.Value);

  // Sortierung nach Schlüsseln und Werten
  ODict.SortByKeys;
  Writeln('Nach Schlüsseln sortiert:');
  for var APair in ODict do
    Writeln(APair.Key + ' - ' + APair.Value);

  ODict.SortByValues;
  Writeln('Nach Werten sortiert:');
  for var APair in ODict do
    Writeln(APair.Key + ' - ' + APair.Value);

  // Benutzerdefinierte Sortierung nach Zahl-Worten
  // Wichtig: In-Place-Create vermeiden. Stattdessen Variable verwenden 
    var GNumberComparer: IComparer<string> := TNumberWordComparer.Create;
    ODict.SortByKeys(GNumberComparer);
  Writeln('Sortiert nach Zahl-Worten:');
  for var APair in ODict do
    Writeln(APair.Key + ' - ' + APair.Value);
end.

Besonderheiten und Flexibilität

In unserem Beispiel nutzen wir SortByKeys und SortByValues, um Elemente nach Schlüsseln und Werten zu sortieren. TOrderedDictionary bietet uns somit eine bequeme Möglichkeit, die Sortierreihenfolge nach Bedarf zu ändern. 

Besonders interessant ist die Möglichkeit, einen benutzerdefinierten IComparer zu implementieren. In diesem Fall sortieren wir nach englischen Zahlenwörtern (onetwothree usw.) mithilfe von TNumberWordComparer.

Dessen Methode GetNumberValue durchsucht ein Array mit Zahlenwörtern, um den Index eines Worts zu finden. Dieser Index wird als numerischer Wert für die Sortierung verwendet. Falls ein Wort nicht gefunden wird, gibt die Funktion MaxInt zurück, wodurch es am Ende der Liste landet. Das Array lässt sich beliebig erweitern. Die Verwendung eines in Delphi 12 eingeführten Multi-Line-Strings macht das Intialisieren des Arrays über die Split-Funktion leicht wartbar.

Wichtig: Der Comparer wird nur beim konkreten Sortieren verwendet und nicht etwa implizit, wenn neue Paare eingefügt werden. Das Dictionary bleibt also weiterhin eine statische Datenstruktur. 

Fazit

Die neue TOrderedDictionary-Klasse ist eine interessante Erweiterung der System.Generics.Collections-Unit in Delphi 12.2. Mit ihrer Fähigkeit, die Reihenfolge von Einfügungen beizubehalten und eine flexible Sortierung durchzuführen, bietet sie Entwicklern eine vielseitige und praktische Lösung für die Verwaltung von Schlüssel-Wert-Paaren.

Code

Type-Inference in Generic Methods

Due to an interesting code example, behind which Kim Madsen first suspected a possible bug in the Delphi compiler, I took a closer look at the calling behavior of overloaded methods that also contain generic type parameters. I was surprised by the possible bug since I use overloads with combinations of generic and non-generic methods in current projects without any obvious errors.

Here is a simplified version of Kim’s code:

TFoo = class(TObject)
  public
    function DoSomething(SomeObject: TValue): string; overload;
    function DoSomething<T>(SomeObject: T): string; overload;
  end;

TBar = class(TObject)
end;

//...

var Foo := TFoo.Create;
var Bar := TBar.Create;

Foo.DoSomething(Bar);

Which of the two possible variants of DoSomething should be called by Foo.DoSomething(Bar)?

At a first glance, I was inclined to say that the non-generic variant, i.e. function DoSomething(SomeObject: TValue): string; should be called, since after all no type parameter is specified and therefore the first variant matches best.

But in fact, the generic, second variant is called. I initially suspected „TValue“ of „blocking“ the overload resolution here, since TValue is actually an „artificial“ RTTI.driven type.

Stefan Glienke came up with the correct hint:

Generic parameters are actually optional and the type is automatically inferred by the type of the variable that is passed. This „type inference“ takes precedence over overload resolution and thus produces a better match than the „TValue“ method.

Type-inference of generic methods is documented here btw.

So the following code actually calls a generic implementation, although no type parameter is specified in the call:

function DoSomething<T>(SomeObject: T): string;
//...
Foo.DoSomething(Bar); //<T> is inferred from "Bar"

Here is a Gist with full code to try:

https://gist.github.com/omonien/24b9b4ba64d46bdfa478615e8e9b0ae6

MVP des Jahres 2019 – DACH Region – Das Ergebnis

Wir haben alle Stimmen der, in der vergangenen Woche durchgeführten, Abstimmung zum MVP des Jahres 2019 für die deutschsprachige Region ausgezählt und der Gewinner steht nun fest:

MVP Emblem

Uwe Raabe

Nach dem klar war, dass Uwe die Abstimmung gewonnen hatte, ist er sozusagen vom Wahl-Komitee zurückgetreten, so dass mir nun die Aufgabe bleibt, Uwe zu würdigen.

Zunächst einmal möchte ich erwähnen, dass Uwe mit 32% der Stimmen deutlich vor dem Zweitplatzierten lag, der 21% der Stimmen erzielen konnte.

Uwes Wahl ist sicherlich ein Resultat seines unermüdlichen Einsatzes, um der Delphi-Community seine beiden wichtigsten Tools, MMX Code Explorer und Project Magician kostenlos zur Verfügung stellen zu können. Beide Tools gehören sozusagen zum Standard-Werkzeugkasten eines jeden Delphi Entwicklers.

Daneben ist Uwe auch regelmäßig in Webinaren, insbesondere bei der Coderage anzutreffen.

Ich habe Jim McKeeth nun Uwe als unseren Kandidaten für den weltweiten MVP des Jahres 2019 vorgeschlagen.

Also: Daumen drücken 🙂

Embarcadero MVP des Jahres 2019 – DACH Region

Uwe Raabe und ich sind die MVP Koordinatoren der DACH Region und haben u.a. die Aufgabe, einen MVP des Jahres auszuwählen.

Da wir dies nicht irgendwie ausklüngeln wollen, haben wir uns entschlossen, der Delphi Community die demokratische Möglichkeit zu geben, den MVP des Jahres zu wählen.

Auf dieser Seite könnt ihr ab sofort, bis zum kommenden Freitag, den 13. Dezember 2019, eure Stimme für den MVP abgeben, der euch im – schon fast vergangenen – Jahre 2019 am meisten beeindruckt hat, dessen Blogs ihr am informativsten fandet oder von dem ihr einfach denkt, er soll Titel für dieses Jahr erhalten.

Was gewinnt der MVP des Jahres? Ganz viel Ruhm – mit „h“ 🙂

Jim McKeeth, sozusagen der MVP Oberaufseher, wird entsprechende Posts verfassen und den Gewinner damit entsprechend würdigen. Flugreisen, Fernseher oder Millionen von Dollar zu spendieren, davon konnten wir Jim leider nicht überzeugen …

Die Abstimmung erfolgt anonym. Jeder, der sich der Embarcadero Community zugehörig fühlt, hat eine Stimme. MVPs können selbstverständlich mit abstimmen und dürfen sich natürlich auch selbst wählen.

Uwe und ich sind auch wählbar – auch wenn wir uns hier eher als Organisatoren sehen.

Das Ergebnis werden wir nach Ablauf der Abstimmung hier veröffentlichen. Wir werden nur den Sieger benennen, also kein Ranking erstellen.

Update: die Abstimmung ist inzwischen beendet. Das Ergebnis findet sich hier.

Publishing Delphi Apps to the Microsoft Store Episode 2

As promised my first video tutorial about the Microsoft Store, here are some additional steps I would like to share with you:

  • Publishing 32 & 64-bit packages
  • Version numbers
  • MS really reads Privacy URLs 🙂

This is the link to version 1.0.1 of HashExpert.

Publishing Delphi Apps to the Microsoft Store Episode 2

As promised my first video tutorial about the Microsoft Store, here are some additional steps I would like to share with you:

  • Publishing 32 & 64-bit packages
  • Version numbers
  • MS really reads Privacy URLs 🙂

This is the link to version 1.0.1 of HashExpert.

From REST to Database

There are frequently questions about how to get the results of a REST query (JSON) into a database table. This can of course be done manually, by using the „while not EOF do“ approach, but there are actually components in Delphi that make this job relatively easy and flexible.

So I created a video, demonstrating how to take the JSON response of a REST request and insert it into a database table.

I am using TFDMemTable, TFDQuery, and TFDBatchmove to move the data from the original TRESTResponse into an SQLite table – just by using FireDAC components.

Source code is available on BitBucket.

From REST to Database

There are frequently questions about how to get the results of a REST query (JSON) into a database table. This can of course be done manually, by using the „while not EOF do“ approach, but there are actually components in Delphi that make this job relatively easy and flexible.

So I created a video, demonstrating how to take the JSON response of a REST request and insert it into a database table.

I am using TFDMemTable, TFDQuery, and TFDBatchmove to move the data from the original TRESTResponse into an SQLite table – just by using FireDAC components.

Source code is available on BitBucket.

Publishing Delphi Apps to the Microsoft Store

In this video, I will guide you through the steps to get your Delphi app published in Microsoft’s App Store. For this demonstration, I will use a Firemonkey app, but technically the same thing would work with VCL apps as well.

This is the appx that I published and got certified by Microsoft. It’s a simple Hash calculator. Nothing too fancy, but useful enough to successfully pass Microsoft’s verification procedures: Microsoft Store – HashExpert

Wir benutzen Cookies um die Nutzerfreundlichkeit der Webseite zu verbessen. Durch Deinen Besuch stimmst Du dem zu.