# 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(listASU);
System.out.println(listRZS);
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>();
else {};
}
Collections.sort(listRZS.get(k));       // sort each row, solution above
k++;
}
```

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.)