How Can I List a TDictionary in Alphabetical Order by Key in Delphi 2009?

Posted by lkessler on Stack Overflow See other posts from Stack Overflow or by lkessler
Published on 2010-03-27T05:36:06Z Indexed on 2010/03/27 5:43 UTC
Read the original article Hit count: 453

How can I use a TEnumerator to go through my TDictionary in sorted order by key?

I've got something like this:

  var
    Dic: TDictionary<string, string>;

  begin
    Dic := TDictionary<string, string>.create;
    Dic.Add('Tired', 'I have been working on this too long');
    Dic.Add('Early', 'It is too early in the morning to be working on this');
    Dic.Add('HelpMe', 'I need some help'); 
    Dic.Add('Dumb', 'Yes I know this example is dumb');

   { The following is what I don't know how to do }
    for each string1 (ordered by string1) in Dic do
      some processing with the current Dic element

    Dic.Free;
  end;

So I would like to process my dictionary in the order: Dumb, Early, HelpMe, Tired.

Unfortunately the Delphi help is very minimal in describing how TEnumerator works and gives no examples that I can find. There is also very little written on the web about using Enumerators with Generics in Delphi.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about delphi-2009