I have an arraylist of arraylist, each inner arraylist contains 2 values, the first is an integer and the second is a string so essentially it would look like: {5, some text}, {12, some more text}, {3, even more text} etc, what I would like to do is take this and sort it so its in descending order of the largest integer to smallest, so the previous example then looks like: {12, some more text}, {5, some text}, {3, even more text}, any help would go a long way thanks in advance
Asked
Active
Viewed 2,192 times
0
-
3Why are these inner things `ArrayList`s, instead of a separate class? – Louis Wasserman May 22 '12 at 13:31
-
I made it this way because the items get added in dynamically tbh, Its all part of a much larger program – Edmund Rojas May 22 '12 at 13:33
-
2Still, `ArrayList` here is awkward, weakly typed, and using a new class for it would make the sorting part significantly easier. – Louis Wasserman May 22 '12 at 13:34
-
what would be a better way to do it? – Edmund Rojas May 22 '12 at 13:35
-
Use a new class for it, and then make that class implement `Comparable` based on the integer key. – Louis Wasserman May 22 '12 at 13:35
-
http://www.coderanch.com/t/385276/java/java/sorting-arraylist-arraylist and http://stackoverflow.com/questions/4699807/sort-arraylist-of-array-in-java might be helpful. – Kazekage Gaara May 22 '12 at 13:36
-
Though @LouisWasserman's method is much much better. – Kazekage Gaara May 22 '12 at 13:37
-
added the 'Comparable' solution down together with a 'main' method for you to test and see the sorting is ok... – lithium May 22 '12 at 13:54
2 Answers
6
your data structure sounds like it is a 'Map' actually. Maybe you should look into data structures, and collection interfaces and classes in particular...
If you still think that what you have is a 'List', then you should make a Collections.sort operation with right kind of Comparator or Comparable
Here is a solution to your data structure if the List is the right one;
import java.util.ArrayList;
import java.util.Collections;
public class InnerObject implements Comparable<InnerObject> {
Integer index;
String name;
public InnerObject(Integer index, String name) {
this.index = index;
this.name = name;
}
@Override
public int compareTo(InnerObject other) {
return index.compareTo(other.index);
}
@Override
public String toString() {
return "{" + index + "," + name + "}";
}
public static void main(String[] args) {
ArrayList<InnerObject> list = new ArrayList<InnerObject>();
list.add(new InnerObject(666, "devil"));
list.add(new InnerObject(1, "one"));
list.add(new InnerObject(10, "ten"));
System.out.println("list before order: " + list);
Collections.sort(list);
System.out.println("list after order: " + list);
}
}

lithium
- 995
- 7
- 13
-
I totally agree with this. It looks more of a map than a list of lists... have a look at Java's Treemap, a map ordered by keys: http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html – Korgen May 22 '12 at 13:56
-
This seems to be exactly what I need, only 2 things I need to figure out now is how to reverse the list so it returns in biggest to smallest and how do I get the second value back out? – Edmund Rojas May 22 '12 at 14:47
-
in the compareTo method, if you write 'return other.index.compareTo(index);' then the order is reversed... Check for null conditions somewhere though, either in compareTo or somewhere up in the logic... – lithium May 22 '12 at 14:50
-
oh no, it appears that the sort function actually didnt sort the numbers in order, they still wind up just staying in the same order they went in – Edmund Rojas May 22 '12 at 15:23
-
are you sure? coz it should be as follows; ' - list before order: [{666,devil}, {1,one}, {10,ten}] - list after order: [{1,one}, {10,ten}, {666,devil}] ' – lithium May 22 '12 at 15:35
-
unfortunately yes, one thing I will say is that it keeps telling me to remove the override annotation from the compareTo() object, Im assuming this is probably why, but Im not sure why this is a problem? – Edmund Rojas May 22 '12 at 15:48
-
You can remove them. or change your compiler's compliance level to 1.6... you can safely remove them though... – lithium May 22 '12 at 16:39
0
It will be better if your inner list can contain one object having 2 properties. This object can than be used for sorting.

ejb_guy
- 1,125
- 6
- 6