Thread overview
sort associative array by key
Nov 23, 2012
dsmith
Nov 23, 2012
Andrej Mitrovic
Nov 23, 2012
Timon Gehr
Nov 23, 2012
dsmith
Nov 23, 2012
Timon Gehr
November 23, 2012
What is the best way to have a function sort an associative array by key?  The following yields a conversion error.

double[string] aa_sort(double[string] aa) {
  return aa.keys.sort;
}


November 23, 2012
On 11/23/12, dsmith <ds@nomail.com> wrote:
> What is the best way to have a function sort an associative array by key?  The following yields a conversion error.
>
> double[string] aa_sort(double[string] aa) {
>    return aa.keys.sort;
> }

Hashes are unordered, you can't sort them by key because they don't preserve any order during insertion/removal. You can alternatively return a sorted array:

string[] keys_sorted(double[string] aa) {
    string[] keys = aa.keys;
    sort(keys);  // from std.algorithm;
    return keys;
}
November 23, 2012
On 11/23/2012 07:09 PM, dsmith wrote:
> What is the best way to have a function sort an associative array by
> key?  The following yields a conversion error.
>
> double[string] aa_sort(double[string] aa) {
>    return aa.keys.sort;
> }
>
>

A hash table is unsorted by definition. What is it that you want to do exactly?

The following will generate a newly allocated dynamic array of key-value pairs, sorted by key:

import std.algorithm, std.typecons;

Tuple!(string, double)[] aa_sort(double[string] aa){
    typeof(return) r=[];
    foreach(k,v;aa) r~=tuple(k,v);
    sort!q{a[0]<b[0]}(r);
    return r;
}
November 23, 2012
On Friday, 23 November 2012 at 18:24:07 UTC, Timon Gehr wrote:
> On 11/23/2012 07:09 PM, dsmith wrote:
>> What is the best way to have a function sort an associative array by
>> key?  The following yields a conversion error.
>>
>> double[string] aa_sort(double[string] aa) {
>>   return aa.keys.sort;
>> }
>>
>>
>
> A hash table is unsorted by definition. What is it that you want to do exactly?
>
> The following will generate a newly allocated dynamic array of key-value pairs, sorted by key:
>
> import std.algorithm, std.typecons;
>
> Tuple!(string, double)[] aa_sort(double[string] aa){
>     typeof(return) r=[];
>     foreach(k,v;aa) r~=tuple(k,v);
>     sort!q{a[0]<b[0]}(r);
>     return r;
> }

Suppose the string is of the format 201207, 21208, ...
So aa["201207"] == 123.45

How do you parse the tuple for the key and the value?
November 23, 2012
On 11/23/2012 07:48 PM, dsmith wrote:
> On Friday, 23 November 2012 at 18:24:07 UTC, Timon Gehr wrote:
>> On 11/23/2012 07:09 PM, dsmith wrote:
>>> What is the best way to have a function sort an associative array by
>>> key?  The following yields a conversion error.
>>>
>>> double[string] aa_sort(double[string] aa) {
>>>   return aa.keys.sort;
>>> }
>>>
>>>
>>
>> A hash table is unsorted by definition. What is it that you want to do
>> exactly?
>>
>> The following will generate a newly allocated dynamic array of
>> key-value pairs, sorted by key:
>>
>> import std.algorithm, std.typecons;
>>
>> Tuple!(string, double)[] aa_sort(double[string] aa){
>>     typeof(return) r=[];
>>     foreach(k,v;aa) r~=tuple(k,v);
>>     sort!q{a[0]<b[0]}(r);
>>     return r;
>> }
>
> Suppose the string is of the format 201207, 21208, ...
> So aa["201207"] == 123.45
>
> How do you parse the tuple for the key and the value?

tpl[0] is the key and tpl[1] is the value in this case.