Iterate generic collection top down (TDictionary)
collections, delphi, delphi-xe, sorting
Solution
The equivalent Delphi code to your Cocoa code is:
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 then this will not be the most efficient solution. On the other hand, it's probably the quickest and easiest approach to implement.
Problem
I have a TDictionary. It's being filled with an extensive loop. When the loop finishes I need to retrieve the 10 keys(string) with more score(integer). What would be the most efficient way to accomplish this? In Objective-C(Cocoa) I do it with: ``` NSArray *top_words_sorted_array = [top_words_dictionary keysSortedByValueUsingSelector:@selector(compare:)]; ``` and then iterating the new sorted array. How can I do it in Delphi?