2

i have a list of lists

private LinkedList<LinkedList<Double>> data = 
new LinkedList<LinkedList<Double>>();

Those lists represent columns(outer list) and rows(inner list). Now i'd like to sort the rows by the values of column 0.

EDIT

New example:

row0 = [4,2]

row1 = [1,3]

After sort it should look like this.

row0 (old row1) = [1,3]

row1 (old row0) = [4,2]

I tried this: creating new Columns and adding values. data.get() indexes a column data.get().get() indexes a row

for (int i = 0; i < 2; i++) {
    LinkedList<Double> newCol = new LinkedList<Double>();
    data.add(newCol);
}
data.get(0).add(4D);
data.get(0).add(1D);
data.get(1).add(2D);
data.get(1).add(3D);

To sort i wrote this code

Collections.sort(data, new Comparator<List<Double>>()
            {
              public int compare(List<Double> o1, List<Double> o2)
              {
                @Overrides
                return o1.get(0).compareTo(o2.get(0));
              }
            });

But this code sorts the columns by the values of row0. That is not what i want.

System.out.println(data.get(0).get(0) + " " + data.get(1).get(0));
System.out.println(data.get(0).get(1) + " " + data.get(1).get(1));

The output is:

row0 = [2, 4]

row1 = [3, 1]

Anyone knows how to do this?

2
  • Can you provide your Comparator's code ? That seems the right way to do it, so you must have gotten wrong somewhere. Commented May 8, 2014 at 21:01
  • i was only able to write a code that sort the cols by the values of row 0. But i want it the other way round.. Commented May 8, 2014 at 21:19

3 Answers 3

2

Try that

public void testSO() {
    LinkedList<LinkedList<Double>> data =
            new LinkedList<>();

    LinkedList l1 = new LinkedList<Double>();
    l1.add(1D);
    l1.add(5D);
    l1.add(3D);

    LinkedList l2 = new LinkedList<Double>();
    l2.add(0D);
    l2.add(3D);
    l2.add(1D);

    LinkedList l3 = new LinkedList<Double>();
    l3.add(2D);
    l3.add(3D);
    l3.add(1D);

    data.add(l1);
    data.add(l2);
    data.add(l3);

    System.out.println("Before sorting :");
    for (LinkedList<Double> list : data) {
        for (Double d : list) {
            System.out.print(d + " ");
        }
        System.out.println();
    }

    Collections.sort(data, new Comparator<LinkedList<Double>>() {
        @Override
        public int compare(LinkedList<Double> o1, LinkedList<Double> o2) {
            try {
                return o1.get(0).compareTo(o2.get(0));
            } catch (NullPointerException e) {
                return 0;
            }
        }
    });

    System.out.println("After sorting :");
    for (LinkedList<Double> list : data) {
        for (Double d : list) {
            System.out.print(d + " ");
        }
        System.out.println();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use a comparator Sorting a list of non-comparable elements This is the complete example you are looking for.

public class Test {
    public static void main(String args[])
    {
        LinkedList<LinkedList<Double>> data =
                new LinkedList<LinkedList<Double>>();
        LinkedList<Double> row1 = new LinkedList<>();
        row1.add(1d); row1.add(5d); row1.add(3d);
        LinkedList<Double> row2 = new LinkedList<>();
        row2.add(0d); row2.add(3d); row2.add(1d);
        LinkedList<Double> row3 = new LinkedList<>();
        row3.add(2d); row3.add(3d); row3.add(1d);
        data.add(row1); data.add(row2); data.add(row3);
        System.out.println("Before the sort:"+ data);
        Collections.sort(data, new Comparator<LinkedList<Double>>() {
            @Override
            public int compare(LinkedList<Double> o1, LinkedList<Double> o2) {
                return (int)(o1.get(0) - o2.get(0));
            }
        });
        System.out.println("After the sort:"+ data);
    }
}

1 Comment

What are these called? anonymous classes or something?
0

Use a comparator with the following compare method:

public int compare(List<Double> l1, List<Double> l2){
     return l1.get(0).compareTo(l2.get(0));
}

Add to this handling of empty lists as you see fit.

2 Comments

this code is similar to mine. But it sorts the columns by the values of row 0. I want it to sort the rows by the values of column 0.
There is somehing wrong with the second example: row1 is [3, 1] compared to [1, 3] in the previous example. Hence it is ordered as "larger" that [2, 4]. If you want to always sort by the least element in the list, sort the inner lists first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.