Reputation: 7547
I have a TDictionary<String, TDateTime>
like this
aDict := TDictionary<String, TDateTime>.Create;
try
aDict.Add('Foo', StrToDateTime('2016-10-14 15:00:00'));
aDict.Add('Bar', StrToDateTime('2016-10-14 14:00:00'));
aDict.Add('Baz', StrToDateTime('2016-10-14 13:00:00'));
finally
aDict.Free;
end;
if i loop on the dictionary like this:
for aKey in aDict.Keys do
WriteLn(aKey );
the output is:
Bar
Baz
Foo
The default order seem based on the key alphabetically, i want sort the dictionary from the oldest to the newest TDateTime on the value. the expected output is:
Baz
Bar
Foo
Any suggestion?
Upvotes: 2
Views: 2416
Reputation: 613003
A dictionary is an unordered collection. If it appears ordered in any particular way, that is purely down to chance. The order of the items is not defined in any way.
If you wish to order these items, transfer them to an array (TArray<string>
) or a list (TList<string>
or TStringList
) and order them there.
Upvotes: 4