Sorted output from an associative array

bearophile bearophileHUGS at lycos.com
Wed Jan 30 08:25:56 PST 2013


> Tuple!(string, int)[] items;
> foreach (k, v; wordCount)
>     items ~= tuple(k, v);
> items.schwartzSort!(it => it[1], "a < b")();

A little tested:

import std.stdio, std.algorithm, std.typecons;

void main() {
     uint[string] wordCount = ["the":200, "val":100, "blue":1000];
     auto items = new Tuple!(string, uint)[wordCount.length];
     uint i = 0;
     foreach (k, v; wordCount)
         items[i++] = tuple(k, v); // avoided append
     items.schwartzSort!(it => it[1], "a < b")();
     writeln(items);
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list