1

I have a list of objects, these objects are actually chess pieces.

Each object contains the name of the price and it position on the chess table. Names are K for king, Q for queen, R for rook... and so on.

So I have an ArrayList<Enemy> chesspieces. The list is not sorted, elements might be something like this:

P,P,P,P,P,P,P,P,R,N,B,Q,K,B,N,R.

I would like to create some sort of a prioritySort, to have a list like this:

K,Q, R, R, B, B, N,N,R,R P,P,P,P,P,P,P,P

I started doing something, but I see how it's flawed and I am not sure how to implement that, here is what I did so far

here is my updated Enemy class



public class Enemy implements Comparable {

        public Piece name;
        public int rank;
        public int file;
        public String position;
        private int value;

        public Enemy(Piece name, int file, int rank,  String position) {
            this.name = name;
            this.rank = rank;
            this.file = file;
            this.position = position;
        }


    public int getValue(Piece name) {
        if (name.toString() == "k") value = 0;
        if (name.toString() == "Q") value = 1;
        if (name.toString() == "R") value = 2;
        if (name.toString() == "B") value = 3;
        if (name.toString() == "N") value = 4;
        if (name.toString() == "R") value = 5;
        if (name.toString() == "P") value = 6;
        System.out.println("ENMIY : " + name.toString() + " threat" + value);
        return value;
    }


    @Override
    public int compareTo(Object o) {
        if (o instanceof Enemy) {
            Enemy other = (Enemy)o;

            return this.value - other.value;
        } else {
            return 0;
        }
    }
}

here is my output

Collections.sort(enemyLocation);// PPNPPPPPPRNBQKBR

2 Answers2

5

Use below code:

Collections.sort(YourList, YourComparator);

Create Comparator and put your logic there in int compare(T o1, T o2)

    Collections.sort(list, new Comparator<Piece>() {

        @Override
        public int compare(Piece o1, Piece o2) {
            // Your logic 
            //a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
            return Your-Return-Value;
        }
    });

Check few example here Comparator Interface in Java

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
  • Initializing your arrest will solve your first problem. – R0b1n Jan 27 '20 at 05:18
  • I am not sure how I can use that, or implement the logic, I get a lot of error saying cannot resolve symbole sort –  Jan 27 '20 at 05:42
  • Double check you are using `Collections.sort()` not `Collection.sort()` (notice the plural Collection**s**) – cameron1024 Jan 27 '20 at 05:46
  • @DavidShapero1. for error, import the class java.util.Collections. 2. for implementation there can be multiple way, you can put if condition and return based on that, or add new property in you class as priority and add int value and use that in compare method. Check the Examples also from the link. – Sumit Singh Jan 27 '20 at 05:52
2

In addition to Sumit Singh's answer, you can also implement the Comparable interface on your Enemy or Piece classes, and use the Collections.sort(...) method to sort your collection. Here's an example.

public class Piece implements Comparable {

    private String symbol;
    private int value;

    public Piece(String symbol, int value) {
        this.symbol = symbol;
        this.value = value;
    }

    @Override
    public String toString() {
        return "Piece{" +
                "symbol='" + symbol + '\'' +
                ", value=" + value +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Piece) {
            Piece other = (Piece)o;

            return this.value - other.value;
        } else {
            return 0;
        }
    }
}

And then, sort your list.

    List<Piece> pieces = new ArrayList<>();
    pieces.add(new Piece("R", 5));
    pieces.add(new Piece("P", 1));
    pieces.add(new Piece("Q", 9));
    pieces.add(new Piece("K", 10));
    pieces.add(new Piece("R", 5));
    pieces.add(new Piece("P", 1));

    Collections.sort(pieces);

    System.out.println(pieces);  //Prints [Piece{symbol='P', value=1}, Piece{symbol='P', value=1}, Piece{symbol='R', value=5}, Piece{symbol='R', value=5}, Piece{symbol='Q', value=9}, Piece{symbol='K', value=10}]
Riaan Nel
  • 2,425
  • 11
  • 18
  • thank you, I tried implementing it in my Enemy class, I created a function, get value, that will return an int, 0-6, please take a look to it, and advise how I could finish it. –  Jan 27 '20 at 06:09
  • You have a method to calculate the value, but you never assign it to your value variable - so it's always going to be zero. Set your 'value' variable in the Enemy constructor - this.value = getValue(...). – Riaan Nel Jan 27 '20 at 06:30
  • Omg, it worked, you're the king, I love you, I am going to do more research to understand it better, but if you could explain the part ```public int compareTo(Object o) { ```that would be awesome –  Jan 27 '20 at 06:33
  • That method comes from the Comparable interface (https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html). It indicates that an object can be compared to another object of the same type - and it can be greater than (result > 0), less than (result < 0), or equal (result == 0). Collections of objects that are comparable can be sorted via the Collections.sort(...) method. – Riaan Nel Jan 27 '20 at 06:37