I have a TDictionary declared this way TDictionary<String,Integer>. Now I want to get the maximum value stored in the TDictionary. I can do this by iterating over TDictionaryand comparing values, but I wonder if there is a better way to do this?exist any function or maybe the dictionary can be sorted by the values to retrieve the max value stored?
this is what i am doing now
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;
source
share