2

Sorry this has probably been answered somewhere but I'm pretty new to Java and I can't apply other answers I have found to the work I'm doing.

So I have an ArrayList which stores locations of pieces it can take on that particular turn.

ArrayList<Move> legalTakes = new ArrayList<Move>();

A move has a co-ordinate from (x1,y1) and a co-ordinate to (x2,y2) which is where the piece it will be taking is at.

moveInput = new Move(pieceToMove, x1, y1, x2, y2, false);

From calling a method (getPiece) I can take the x2,y2 and see which piece is at that location and find the value of that piece.

for (Move m : legalTakes) {  //for all of the possible takes that turn...
    Piece occPieceToTake = this.getBoard().getPiece(m.getX2(),m.getY2());
    int pieceValue = this.getBoard().getPiece(m.getX2(),m.getY2()).getValue(occPieceToTake.getChar());
    System.err.println("-- WORTH : "+pieceValue);           
}

From this is it possible to order my ArrayList based on the value returned by pieceValue? So if a king is returned with a value of 6, I can select that over a pawn which will return a value of 1?

Hope this is sufficient information..if not please let me know.

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115

2 Answers2

0

You can use a comparator for this. First, you need to create a class implementing the Comparator interface. Then you can use the Collections utility class:

Collections.sort(list, new MyComparator());
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
0

you can sort you list by doing this,

ArrayList<ListObj> list = new ArrayList<ListObj>();

   Collections.sort(list, Comparator);
//or 
       Collections.sort(list);  

have a look on comparator if you are unsure about its working.

Muneeb Nasir
  • 2,414
  • 4
  • 31
  • 54