What is the best way to find the maximum value in delphi TDictionary?

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;
+5
source share
4 answers

- ? , TDictionary, Add() , . . (, , Add() , , , ). . , : , . , , fLargestWordSoFar fLargestCountSoFar.

, , , Add().

type
  MyTDictionary = object(TDictionary) // almost definitely not correct syntax here...
  private
    fLargestCountSoFar: Integer;
    fLargestWordSoFar: String;   
  public
    procedure Add( S: String; I:Integer); override;   
  end;

implementation

procedure MyTDictionary.Add( S: String; I:Integer); 
begin
  if (I > fLargesteCountSoFar) then
  begin
    fLargestCountSoFar := I;
    fLargestWordSoFar  := S;    
  end;
  inherited Add( S, I);
 end;
+2

TDictionary , - .

.

+3

, Delphi Collections 1.1.1. TDoubleSortedBidiDictionary, .

: AVL . , .

btw, " ", TBag Delphi Collections. Delphi MultiSet.

+3

- , - . , . tstringlist - .

MaxIntValue (MyDict.ToArray) Math-unit , . , , .

+2

All Articles