How to efficiently resolve Associative Arrays not being sorted?

Ali Çehreli acehreli at yahoo.com
Tue Jun 2 09:02:36 UTC 2020


On 6/2/20 12:32 AM, BoQsc wrote:

 > I want to read a file, put it into an array, make some search and
 > replace on the content and output the modified text.

How large is the data? If it fits into memory, just read the whole 
thing, update it, sort the keys, and then output like this:

   import std.stdio;
   import std.algorithm;

   foreach (key; aa.keys.sort) {
     writeln(key, aa[key]);
   }

Of course, you can save the sorted array in a local variable as well if 
you will use it again:

   auto keys = aa.keys.sort;

One great thing about programs that read and write files is that unless 
the data is so large that it does not fit into physical memory, you 
can't feel the time cost of that sort operation. :)

Ali



More information about the Digitalmars-d-learn mailing list