You want a custom comparator to compare lists of lists of integers.
public class ListComparator implements Comparator<List<Integer>> {
@Override
public int compare(List<Integer> list1, List<Integer> list2) {
int value1 = list1.get(0);
int value2 = list2.get(0);
return Integer.compare(value1, value2);
}
}
Then you pass it in as the second argument to sort.
Collections.sort(demoList, new ListComparator());
EDIT - here is a version that handles null and empty lists in the list being sorted.
public class ListComparator implements Comparator<List<Integer>> {
@Override
public int compare(List<Integer> list1, List<Integer> list2) {
int result = 0;
if (list1 == null) {
// define null as equal to null and less than everything else
result = (list2 == null) ? 0 : -1;
}
else if (list1.isEmpty()) {
// define empty as greater than null, equal to empty, and less than non-empty
result = (list2 == null) ? 1 : (list2.isEmpty() ? 0 : -1);
}
else if (list2 == null || list2.isEmpty()) {
// define non-empty (list1) as greater than null or empty
result = 1;
}
else {
// both are non-empty so compare the first values from each
int value1 = list1.get(0);
int value2 = list2.get(0);
result = Integer.compare(value1, value2);
}
return result;
}
}