3

我有一本 TDictionary。它充满了广泛的循环。当循环完成时,我需要用更多的分数(整数)检索 10 个键(字符串)。实现这一目标的最有效方法是什么?

在 Objective-C(Cocoa) 中,我这样做:

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

然后迭代新的排序数组。我怎样才能在德尔福中做到这一点?

4

2 回答 2

5

与您的 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
  )
);

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.

于 2012-04-26T15:26:08.050 回答
0

您需要以地图(字典)的形式访问它,还是一个普通的数组就足够了?

如果您必须将其作为地图,我会看一下DeHL.Collections,也许可以满足DeHL.Collections.DoubleSortedBidiMap您的需求。项目页面说它已停产,但我每天都使用它,从来没有问题。

于 2012-04-26T14:47:55.360 回答