Functional programming 为什么.group之后需要.array来使用.sort?

Functional programming 为什么.group之后需要.array来使用.sort?,functional-programming,d,Functional Programming,D,具有以下代码: import std.algorithm : filter, canFind, map, splitter, group, sort; import std.stdio : File, writefln; import std.range : array; void main(string[] args) { string filename = "/var/log/dpkg.log"; string term = args[1]; auto resul

具有以下代码:

import std.algorithm : filter, canFind, map, splitter, group, sort;
import std.stdio : File, writefln;
import std.range : array;

void main(string[] args)
{
    string filename = "/var/log/dpkg.log";

    string term = args[1];
    auto results = File(filename, "r")
                    .byLine
                    .filter!(a => canFind(a, term))
                    .map!(a => splitter(a, ":").front)
                    .group
                    .array  // why is this crucial ?
                    .sort!((a,b) => a[1] > b[1]);

    foreach (line; results)
        writefln("%s => %s times", line[0], line[1]);
}
我发现我非常需要
.group
之后的
.array
。有人能告诉我为什么吗

一旦我摆脱它,就会出现以下编译器错误:

main.d(16): Error: template std.algorithm.sorting.sort cannot deduce function from argument types !((a, b) => a[1] > b[1])(Group!("a == b", MapResult!(__lambda3, FilterResult!(__lambda2, ByLine!(char, char))))), candidates are:
/usr/include/dmd/phobos/std/algorithm/sorting.d(1830):        std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range)
main.d(16):错误:模板std.algorithm.sorting.sort无法从参数类型推断函数!((a,b)=>a[1]>b[1])(组!((a==b,MapResult!(u lambda3,FilterResult!(u lambda2,署名!(char,char‘)))))),候选对象为:
/usr/include/dmd/phobos/std/algorithm/sorting.d(1830):std.algorithm.sorting.sort(别名less=“a
组的结果是一个延迟计算的序列,但是
排序
要求其全部输入完全在内存中,例如数组。
array
函数获取由
group
生成的惰性序列,并将其存储到一个数组中,
sort
能够执行操作。

太棒了!这正是我想要的。请注意,
group
也取决于排序:将连续相等的元素分组为元素的单个元组及其重复次数。