Let's say i have an array: int[string] wordCount.
How to print key:value pairs ordered by descending value?
Or generally how to to store wordCount in an array of structs or type tuples for
later sorting?
In Python I would use something like this:
sorted(wordCount.items(), key=lambda a: a[1],
FG:
Let's say i have an array: int[string] wordCount.
That's an associative array, and it's unsorted just like a Python
dict.
In Phobos there is a sorted tree, if you want, that keeps keys
sorted.
How to print key:value pairs ordered by descending value?
There are various solutions.
On Wednesday, 30 January 2013 at 15:43:21 UTC, FG wrote:
Let's say i have an array: int[string] wordCount.
How to print key:value pairs ordered by descending value?
Or generally how to to store wordCount in an array of structs
or type tuples for later sorting?
In Python I would use something
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
On Wednesday, 30 January 2013 at 16:22:37 UTC, monarch_dodra
wrote:
On Wednesday, 30 January 2013 at 15:43:21 UTC, FG wrote:
Let's say i have an array: int[string] wordCount.
How to print key:value pairs ordered by descending value?
Or generally how to to store wordCount in an array of structs
Thanks for showing me how to use tuples in this problem.
Just for the record, the sort comparison should be reversed: a b.