Sorted output from an associative array

monarch_dodra monarchdodra at gmail.com
Wed Jan 30 08:36:58 PST 2013


On Wednesday, 30 January 2013 at 16:22:37 UTC, monarch_dodra 
wrote:
> On Wednesday, 30 January 2013 at 15:43:21 UTC, FG wrote:
>> Let's say i have an array: int[string] wordCount.
>> How to print key:value pairs ordered by descending value?
>> Or generally how to to store wordCount in an array of structs 
>> or type tuples for later sorting?
>>
>> In Python I would use something like this:
>> sorted(wordCount.items(), key=lambda a: a[1], reverse=True).
>
> Basically, you have to extract and sort a third party data 
> structure.
>
> what you can do is extract only the keys though, and sort them 
> according to a specific pred. Then once your keys are sorted, 
> you can re-obtain the value from the original AA.

Yes, as mentioned, you can use schartzsort for better 
performance. Even then though, depending on the size of your 
values, it may be smarter to sort only the keys.


More information about the Digitalmars-d-learn mailing list