digitalmars.D.learn - sort associative array by key
- dsmith (5/5) Nov 23 2012 What is the best way to have a function sort an associative array
- Andrej Mitrovic (9/14) Nov 23 2012 Hashes are unordered, you can't sort them by key because they don't
- Timon Gehr (12/17) Nov 23 2012 A hash table is unsorted by definition. What is it that you want to do
- dsmith (4/25) Nov 23 2012 Suppose the string is of the format 201207, 21208, ...
- Timon Gehr (2/30) Nov 23 2012 tpl[0] is the key and tpl[1] is the value in this case.
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; }
Nov 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; }
Nov 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; }
Nov 23 2012
On Friday, 23 November 2012 at 18:24:07 UTC, Timon Gehr wrote:On 11/23/2012 07:09 PM, dsmith wrote: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?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; }
Nov 23 2012
On 11/23/2012 07:48 PM, dsmith wrote:On Friday, 23 November 2012 at 18:24:07 UTC, Timon Gehr wrote:tpl[0] is the key and tpl[1] is the value in this case.On 11/23/2012 07:09 PM, dsmith wrote: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?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; }
Nov 23 2012