我有一个声明如下的 TDictionary:
以下是我现在所做的:
TDictionary<String,Integer>
,现在我想获取存储在 TDictionary 中值的最大值。我可以通过遍历 TDictionary
并比较值来实现这一点,但我想知道是否存在更好的方法? 是否存在任何函数或者字典可以按值进行排序以检索存储的最大值?以下是我现在所做的:
var
MyDict : TDictionary<String,Integer>;
MaxValue, i : Integer;
begin
MyDict:=TDictionary<String,Integer>.Create;
try
MyDict.Add('this',1);
MyDict.Add('is',7);
MyDict.Add('a',899);
MyDict.Add('sample',1000);
MyDict.Add('finding',12);
MyDict.Add('the',94);
MyDict.Add('max',569);
MyDict.Add('value',991);
MaxValue:=MyDict.ToArray[0].Value;
for i in MyDict.Values do
if i>MaxValue then MaxValue:=i;
ShowMessage(Format('The max value is %d',[MaxValue]));
finally
MyDict.Free;
end;
end;