JAVA: Sort ArrayList<ArrayList<Integer>> on multiple columns

Posted by Bob on Stack Overflow See other posts from Stack Overflow or by Bob
Published on 2012-09-17T21:16:21Z Indexed on 2012/09/17 21:38 UTC
Read the original article Hit count: 609

First, I did do my homework searching before posting here. My requirement seems to be slightly different compared to questions posted out there.

I have a matrix like ArrayList<ArrayList<Integer>> in the following form

| id1 | id2 | score |
|-----|-----|-------|
| 1   | 3   | 95%   |
| 1   | 2   | 100%  |
| 1   | 4   | 85%   |
| 1   | 5   | 95%   |
| 2   | 10  | 80%   |
| 2   | 15  | 99%   |

I want to sort the matrix column-wise (first using score, then the id1). I already have the id1 in a sorted manner. That means I also need to sort all records with the same id1 first by using score, second by the id2. The reason for doing this is to create a ranking of the id2 in each id1. The result for the above example would be:

| q_id | d_id | rank | score |
|------|------|------|-------|
| 1    | 2    | 1    | 100%  |
| 1    | 3    | 2    | 95%   |
| 1    | 5    | 3    | 95%   |
| 1    | 4    | 4    | 85%   |
| 2    | 15   | 1    | 99%   |
| 2    | 10   | 2    | 80%   |

How can I achieve this in Java using some built-in methods of collections?

© Stack Overflow or respective owner

Related posts about java

Related posts about sorting