sort associative array by key

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Nov 23 10:23:24 PST 2012


On 11/23/12, dsmith <ds at 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;
}


More information about the Digitalmars-d-learn mailing list