Mig*_*l E 3 delphi sorting collections delphi-xe
我有一个TDictionary.它充满了广泛的循环.当循环结束时,我需要检索具有更多分数(整数)的10个键(字符串).实现这一目标的最有效方法是什么?
在Objective-C(Cocoa)中,我这样做:
NSArray *top_words_sorted_array = [top_words_dictionary keysSortedByValueUsingSelector:@selector(compare:)];
Run Code Online (Sandbox Code Playgroud)
然后迭代新排序的数组.我怎么能在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
)
);
Run Code Online (Sandbox Code Playgroud)
如果您的字典非常大,那么这将不是最有效的解决方案.另一方面,它可能是最快捷,最简单的实施方法.
| 归档时间: |
|
| 查看次数: |
541 次 |
| 最近记录: |