gpt4 book ai didi

delphi - 自上而下迭代通用集合(TDictionary)

转载 作者:行者123 更新时间:2023-12-03 15:50:44 24 4
gpt4 key购买 nike

我有一本 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com