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.