digitalmars.D.learn - assoc. array sort or preserve order
- Dr. Smith (15/15) Sep 10 2010 Is there a way to preserve an array's original order, or to sort an asso...
- Jesse Phillips (14/16) Sep 10 2010 No, Associative arrays can not preserve this for performance reasons. Th...
Is there a way to preserve an array's original order, or to sort an assoc arr by key? With code like the following, the values of the string indices allow foreach to needlessly re-order the array: ... double[string][string] val; ... foreach(string1, row; val) { foreach(string2, col; row) { writefln("%s\t%s\t%f", string1, string2, col); } } One possible way to control ordering is to add an index, like double[int][string][string] where the int is an index. However, I haven't yet overcome the compiler's problem with the int.
Sep 10 2010
Dr. Smith Wrote:Is there a way to preserve an array's original order, or to sort an assoc arr by key?No, Associative arrays can not preserve this for performance reasons. Though you can do the following. import std.algorithm; import std.stdio; void main() { double [string][string] val; auto keys = val.keys; sort(keys); foreach(key; keys) { foreach(string2, col; val[key]) { writefln("%s\t%s\t%f", val[key], string2, col); } } }
Sep 10 2010