我有一本 TDictionary。它充满了一个广泛的循环。当循环结束时,我需要检索 10 个键(字符串)和更多分数(整数)。实现这一目标最有效的方法是什么?
在 Objective-C(Cocoa) 中,我使用以下方法:
NSArray *top_words_sorted_array = [top_words_dictionary keysSortedByValueUsingSelector:@selector(compare:)];
然后迭代新的排序数组。我怎样才能在Delphi中做到这一点?
最佳答案
与 Cocoa 代码等效的 Delphi 代码是:
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
)
);
如果您的字典非常大,那么这不是最有效的解决方案。另一方面,它可能是最快、最简单的实现方法。
关于delphi - 自上而下迭代通用集合(TDictionary),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10335457/