Say I have a function foo, with some parameters. In this function I generate an array m of type Move. I now need to sort this array. Ok, so I break out Arrays.sort(m, Comparator() { ... }. But wait, the function I want to use to compare two moves needs to take the parameters of foo into account, yet a Comparator's compare function can only take two arguments of type Move! See following code for what I want to do.
public int compare (Move m1, Move m2, Game g, int id) {
return g.evaluate(m1, g, id) - g.evaluate(m2, g, id);
}
public int foo (Game g, int id) {
Move[] m = ... ;
???
// m is now sorted by compare(move1, move2, g, id)
}
I do not have access to the Game class itself, so I can't just edit that to solve my problem. The Comparator class seems unable to do this. Is there any way to actually do this in Java?
Gameand anint, and stores them as instance variables. The MoveComparator can then compare the twoMoves it's passed using theGameandintit already has.Comparatoras an anonymous class, and usegandiddirectly if you make them final.