How to efficiently resolve Associative Arrays not being sorted?

Luis luis.panadero at gmail.com
Tue Jun 2 08:07:45 UTC 2020


On Tuesday, 2 June 2020 at 07:32:56 UTC, 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. 
> However Associative Arrays seem to be unsorted by default. 
> Should I drop the Associative Arrays and use something else? 
> What are the ways to resolve this randomness in Associative 
> Arrays?
>
> ReadfileAndCopyContentIntoArray.d
>> import std.stdio;
>> 
>> int lineNumber = 0;
>> char[][int] fileInArray;
>> 
>> void main(){
>> 	File exampleFile = File("exampleText.txt");
>> 	foreach(line; exampleFile.byLine){
>> 		
>> 		lineNumber++;
>> 		fileInArray[lineNumber] ~= line;
>> 		
>> 	}
>> 	writeln(fileInArray);
>> }
>
> exampleText.txt
>> The quick brown fox jumps over the lazy dog
>> Sphinx of black quartz, judge my vow.
>> How vexingly quick daft zebras jump!
>> The five boxing wizards jump quickly
>> Maecenas consectetur risus a lacus sodales iaculis.
>> Morbi sed tortor sollicitudin, pharetra massa egestas, congue 
>> massa.
>> Sed sit amet nisi at ligula ultrices posuere quis nec est.
>> Mauris vel purus viverra, pellentesque elit id, consequat 
>> felis.
>
> The Command Prompt Output
>> [6:"Morbi sed tortor sollicitudin, pharetra massa egestas, 
>> congue massa.\r", 7:"Sed sit amet nisi at ligula ultrices pos
>> uere quis nec est.\r", 2:"Sphinx of black quartz, judge my 
>> vow.\r", 3:"How vexingly quick daft zebras jump!\r", 1:"The q
>> uick brown fox jumps over the lazy dog\r", 8:"Mauris vel purus 
>> viverra, pellentesque elit id, consequat felis.", 5:"Maec
>> enas consectetur risus a lacus sodales iaculis.\r", 4:"The 
>> five boxing wizards jump quickly\r"]
>
> As can be seen in the Command Prompt Output, the array is not 
> ordered correctly.
> It goes: 6: 7: 2: 3: 1: 8: 5: 4:
> Instead of 1: 2: 3: 4: 5: 6: 7: 8:

Associative arrays looks that uses internally a hash map. Hash 
maps are unordered. The ouput order, looks that is the result of 
where the hash algorithm it's inserting the key/value pair in the 
hashmap.

If you need order, you need to use a TreeMap (D std lib, 
emsi_containers and containersd have TreeMap implementations) or 
something like Java LinkedHashMap if order by insertion it's 
enough.


More information about the Digitalmars-d-learn mailing list