Sort List of List

I have List of Lists(list rows contain list column). I want to sort my rows in ListRZS by length(.size()).

[[123, 189, 277], [11], [145, 211, 299], [156, 222, 310], [167, 233, 255], [189, 266], [200, 277], [211, 288], [245, 299], [233], [244]]

Shoul be:
[[11], [233], [244], [189, 266],[200, 277], [211, 288], [245, 299], [123, 189, 277], [145, 211, 299], [156, 222, 310], [167, 233, 255]]

Question: How to write working comporator/comparable for this situation.

public class Start {
public static void main(String[] args) {
    System.out.println("Start reading from Xls");
    ReaderXls1Column read = new ReaderXls1Column();
    ReaderXls readrzs = new ReaderXls();
    List<String> listASU = new ArrayList<String>(read.ReaderXls1Column("Text1obj",0,1));                        // Creating Lists
    List<List<String>>  listRZS = new ArrayList<List<String>>(readrzs.ReadXls("Text1obj",2,12));
    System.out.println(listASU);
    System.out.println(listRZS);
    System.out.println("Reading is over");
    System.out.println(listRZS);
    WriterXls.main("Text1obj",listRZS);
    System.out.println("Writing is over");
}

My attempt without comparator, comparable(of course, not working right)

        int k=0;
    while ( k<listRZS.size()){
        for (int j=0;j<10;j++)     {
            List<String> tmplist = new ArrayList<String>();
            if (listRZS.get(k).size()>listRZS.get(k+1).size()) {Collections.copy(listRZS.get(k),tmplist); listRZS.remove(k); listRZS.add(k+1,tmplist);}
            else {};
        }
        Collections.sort(listRZS.get(k));       // sort each row, solution above
        k++;
    }

Answer

A comparator just needs to implement one method (compare), that takes two items, and returns:

  • 0 if they are equal
  • positive value if the second is less than the first
  • negative value if the second is greater than the first.

When sorting integer values, a typical pattern to accomplish this is to reduce each object received in the compare method to an integer, then, if we call them a and b, simply return a - b.

Example (see it running at ideone.com/rcmDbi):

import java.util.*;

class Test {

    public static void main(String[] args) {
        List<Integer> a = Arrays.asList(1);
        List<Integer> b = Arrays.asList(1,2);
        List<Integer> c = Arrays.asList(1,2,3);
        List<Integer> d = Arrays.asList(1,2,3,4);

        List<List<Integer>> test = Arrays.asList(d,b,c,a);
        Collections.sort(test, ListSizeComparator.INSTANCE);
        for (List<Integer> list : test) {
            System.out.println(list.size());
        }
    }

    enum ListSizeComparator implements Comparator<List> {
        INSTANCE;

        public int compare(List one, List other) {
            return one.size() - other.size();
        }
    }
}

(By the way, I’ve used an enum for the enum singleton pattern because our Comparator stores no state.)

Leave a Reply

Your email address will not be published. Required fields are marked *