0

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

Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92

2 Answers2

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