user12581183
user12581183

Reputation:

ArrayList Sort by priority

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

Upvotes: 1

Views: 876

Answers (2)

Riaan Nel
Riaan Nel

Reputation: 2535

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}]

Upvotes: 2

Sumit Singh
Sumit Singh

Reputation: 15896

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

Upvotes: 5

Related Questions