Sorting Assosiative Arrays and Finding Largest Common Substring

thedeemon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 16 09:23:25 PDT 2017


On Thursday, 16 March 2017 at 16:02:13 UTC, helxi wrote:
> I was looking for ways to find the largest common substring 
> between two given substrings and have learnt
> 1. .length is of type ulong

Only when compiling to 64 bits. On 32-bit target it's different.

> 2. writing string[int] will not give me a sorted array

Sure, it's just a hash table, its key type may not even have any 
ordering, not be comparable.

> What's the trick to sort the associative array by their keys?

If you need the max key value you can just write
auto k = commons.byKey.maxElement;
so that commons[k] will give you your longest string.
(byKey returns a range of keys, maxElement is a function from 
std.algorithm)

You can use .keys property to get an array of keys of associative 
array so you can sort them if you need.

Btw in your case you don't need an array if you're really 
interested in longest substring, just have a single string 
variable and update it when current candidate is longer.



More information about the Digitalmars-d-learn mailing list