Comment #0 by bearophile_hugs — 2012-07-03T13:16:11Z
This sorts (in reverse) the arrays a and b according to the (a,b) pairs:
import std.stdio, std.algorithm, std.range;
void main() {
auto a = [10, 20, 30];
auto b = ["c", "b", "a"];
writeln(a, " ", b);
sort!q{a > b}(zip(a, b)); // OK
writeln(a, " ", b);
}
Output:
[10, 20, 30] ["c", "b", "a"]
[30, 20, 10] ["a", "b", "c"]
But this code doesn't compile:
import std.stdio, std.algorithm, std.range;
void main() {
auto a = [10, 20, 30];
auto b = ["c", "b", "a"];
writeln(a, " ", b);
topN!q{a > b}(zip(a, b), 4); // error
writeln(a, " ", b);
}
With the latest DMD2.060alpha it gives several errors like:
algorithm.d(6826): Error: template std.algorithm.swap does not match any function template declaration
I think topN doesn't need to ask more features than sort() to the range you give it. So maybe topN too should work here.