【发布时间】:2015-06-11 09:24:32
【问题描述】:
这可能不是最好的主意,但我试图使用 D 的一些内置功能来对关联数组进行排序。这样我就可以对计算的顶部或底部值进行切片。
我试过了:
sort!((a,b) {return query[a] < query[b];})(query);
和:
sort(query);
导致同样的错误:
错误:模板 std.algorithm.sort 无法从参数类型推导出函数!()(int[string]),候选者是: /usr/local/Cellar/dmd/2.066.1/include/d2/std/algorithm.d(9384): std.algorithm.sort(别名少 = "a
这是整个班级:
import std.stdio;
import std.array;
import std.algorithm;
import DataRow;
import LocationMap;
class Database{
this(){ /* intentionally left blank */}
public:
void addRow(DataRow input){ this.db ~= input; }
DataRow[] getDB(){ return this.db; }
DataRow getDBRow(uint i){ return this.db[i]; }
int[string] exportQuery(uint year){
int[string] query;
foreach (DataRow row ; db){
if (row.getYear() == year){
query[row.getCountryName()] = row.getExports;
}
}
//sort!((a,b) {return query[a] < query[b];})(query);
sort(query);
return query;
}
private:
DataRow[] db;
LocationMap locMap;
}
【问题讨论】:
标签: algorithm sorting d associative-array