Iterate/sort associative array by value?

Seb seb at wilzba.ch
Sun Apr 7 17:16:12 UTC 2019


On Sunday, 7 April 2019 at 16:44:01 UTC, Robert M. Münch wrote:
> On 2019-04-07 16:24:52 +0000, Cym13 said:
>
>> You could use sort to gather the indexes in order then 
>> traverse from there:
>> 
>>      aa.byKey.array.sort!((a, b) => aa[a]<aa[b])
>
> That doesn't work: Error: no property array for type Result
>> 
>> With a wrapper caching that order and making it transparent as 
>> well as update on insertion (which should be in log(n) since 
>> you know have an ordered list of indexes, you can use 
>> dichotomy to update the indexes without walking all your AA 
>> again) I think you could have a nice little container. However 
>> if double entry is necessary maybe a simpler 2D array would be 
>> easier to work with?
>
> At the point where I need this sorted array, nothing will 
> change it. It's a log output. So, not necessary to make things 
> more complex.

Then you can do:

---
["a": 1].byPair.array.sort!((a, b) => a.value < 
a.value).release.each!writeln;
---

You'll have a sorted array with key and value props.


More information about the Digitalmars-d-learn mailing list