Top-Down Iteration of a General Collection (TDictionary)

I have a TDictionary. It is filled with an extensive loop. When the loop ends, I need to get 10 keys (string) with a lot of points (integer). What would be the most effective way to do this?

In Objective-C (Cocoa), I do this with

NSArray *top_words_sorted_array = [top_words_dictionary keysSortedByValueUsingSelector:@selector(compare:)];

and then repeat the new sorted array. How can I do this in Delphi?

+3
source share
2 answers

Equivalent Delphi code for your Cocoa code:

type
  TScorePair = TPair<string,Integer>;
var
  ScoresArray: TArray<TScorePair>;
....
ScoresArray := Scores.ToArray;
TArray.Sort(ScoresArray,
  TComparer<TScorePair>.Construct( 
    function(const L, R: TScorePair): Integer
    begin
      Result := R.Value - L.Value;
    end
  )
);

If your dictionary is very large, this will not be the most effective solution. On the other hand, this is probably the fastest and easiest implementation approach.

+5
source

() ?

, DeHL.Collections, , DeHL.Collections.DoubleSortedBidiMap , . , , .

0

All Articles