Ivan-Mark Debono
Ivan-Mark Debono

Reputation: 16340

Java Sort a list based on another list

I have an ArrayList of the following type:

class Move
{
    int from, to;
}

The from property always has a value. The to property will have -1 if it is not set. I have the following array:

int[][] history = new int[50][50];

where the dimensions correspond to the 'from' and 'to' of the move class. In my search function, and depending on certain conditions I need to do:

List<move> moves = board.getMoves();
for (int i = 0; i < moves.size(); i++)
    history[move.from][move.to]++;

Because move.to could also be -1, should I increase the dimension of the 2d array 1 and then do:

history[move.from+1][move.to+]++;

Also, based on the above move list and history array, I need to sort the move list in descending order depending on the counter of the corresponding history index.

Is this possible?

Upvotes: 0

Views: 4481

Answers (3)

Jeroen Vuurens
Jeroen Vuurens

Reputation: 1251

You can setup history as a HashMap or separate class to make this easier. But because you also like to be able to sort history based on frequency, I would recommend a History class:

class Move {

   int from, to;

   @Override
   public int hashCode() {
      return from + (to * 100);
   }

   @Override
   public boolean equals(Object o) {
      return (o instanceof Move
              && ((Move) o).from == from
              && ((Move) o).to == to);
   }
}

class History extends Move implements Comparable<History> {

   int frequency;

   public History(Move m) {
      from = m.from;
      to = m.to;
      frequency = 1;
   }

   public void increment() {
      frequency += 1;
   }

   public int compareTo(History h) {
      // to be able to sort it in a TreeSet descending on frequency
      // note that it is not resorted if you change frequencies, so 
      // build the set, and then convert it to a TreeSet afterwards.
      return (frequency == h.frequency) ? 1 : (h.frequency - frequency);
   }
}

Then create a HashMap to quickly fill history, and convert it into a TreeSet to sort:

  List<Move> moves = board.getMoves();
  HashMap<History, History> fillTable = new HashMap<History, History>();
  for (Move m : moves) {
     History h = fillTable.get(m);
     if (h == null) {
        h = new History(m);
        fillTable.put(h, h);
     } else {
        h.increment();
     }
  }
  TreeSet<History> sorted = new TreeSet<History>(fillTable.values());
  .... ready to use

Upvotes: 0

Vincent van der Weele
Vincent van der Weele

Reputation: 13187

Yes, you can make your comparator that uses the history array. As an example, I sort my list of int according to an other array counts.

public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    list.addAll(Arrays.asList(new Integer[]{0, 1, 2, 3, 4, 5}));
    final int[] counts = new int[] {3, 4, 1, 7, 0, 1};

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

        @Override
        public int compare(Integer arg0, Integer arg1) {
            return counts[arg1] - counts[arg0];
        }
    });

    System.out.println(list);
}

Outputs: [3, 1, 0, 2, 5, 4]

Your compare would be something like:

@Override
public int compare(Move move0, Move move2) {
    return history[move1.from+1][move1.to] - history[move0.from+1][move0.to];
}

Upvotes: 0

pbespechnyi
pbespechnyi

Reputation: 2291

You can use Collections.sort(List, Comparator) with your implementation of Comparator, which will sort as you wish.

Upvotes: 1

Related Questions