Delphi 德尔福词典<;字符串,TDateTime>;按值排序
我有一个像这样的Delphi 德尔福词典<;字符串,TDateTime>;按值排序,delphi,Delphi,我有一个像这样的t字典 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'));
t字典
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;
输出为:
Bar
Baz
Foo
Baz
Bar
Foo
默认顺序似乎是基于按字母顺序排列的键,我希望根据值将字典从最早的TDateTime排序到最新的TDateTime。预期产出为:
Bar
Baz
Foo
Baz
Bar
Foo
有什么建议吗?字典是无序的集合。如果它以某种特定的方式排列,那完全是偶然的。没有以任何方式定义项目的顺序
如果要订购这些项目,请将它们转移到数组(TArray
)或列表(TList
或TStringList
)中并在那里订购。则TDictionary是错误的容器。为列表创建一个记录类型,如TmyRecord=record aname:string aDate:Tdatetime;结束;使用tList并像您希望的那样进行排序。。。我在做TList