JAVA: Sort ArrayList<ArrayList<Integer>> on multiple columns
- by Bob
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?