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

By : Bob
Source: Stackoverflow.com
Question!

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?

By : Bob

First off, you're better holding your data in a Plain Old Java Object (POJO) rather than a List of Lists of Integers.

From your example, it seems as though your data has three elements: two ids and a score. A POJO might be like this:

``````public class Record() {
private int id1;
private int id2;
private double score; // percent, so double from 0.0 to 1.0

public Record(int id1, int id2, double score) {
this.id1=id1;
this.id2=id2;
this.score=score;
}
// getters and setters
}
``````

Then instead of your complicated sub-lists, you'd just have a `List`

``` By : Roddy of the Frozen Peas ```
``` Create an Object that includes all columns/fields of your each ArrayList row. Then make use of Comparator interface and use Collections.sort(). You can check out http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List, java.util.Comparator) By : sharadendu sinha A proper OO solution would be to declare this as class Bucket { int val1; int val2; int percent; } List By : Jim Garrison This video can help you solving your question :) By: admin ```
``` Related Questions Sort on a string that may contain a number Sort a Map<Key, Value> by values (Java Sorting matched arrays in Java Algorithm for merging large files (adsbygoogle = window.adsbygoogle || []).push({}); How do I sort a list of integers using only one additional integer variable? Writing resultset to file with sorted output How to map sorted index back to original index for collection I'm sorting Sort arrays of primitive types in descending order How can I sort class members by name in NetBeans, or another Java IDE? Java Bit Operations (In Radix Sort) Sorted collection in Java Why doesn't java.lang.Number implement Comparable? Alphabetically Sort a Java Collection based upon the 'toString' value of its member items Sortable table-like Java class Convert Collection to List Sort a single String in Java Why is my simple comparator broken? Strategy for detecting an object in JTable row? How can I sort a List alphabetically? What sort does Java Collections.sort(nodes) use? ```
``` ```
``` About Us    Contact Us    Legal    feedback    Copyright © 2015 - All Rights Reserved - www.4answered.com 'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpsh'},{'server':'p3plcpnl0831'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.```