www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sort associative array by key

reply "dsmith" <ds nomail.com> writes:
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
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
parent reply "dsmith" <ds nomail.com> writes:
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?
Nov 23 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Nov 23 2012