這裏是這樣做的方法的一個例子,
// A "size()" comparator
private static Comparator<LinkedList<Long>> comp = new Comparator<LinkedList<Long>>() {
@Override
public int compare(LinkedList<Long> o1, LinkedList<Long> o2) {
return new Integer((o1 == null) ? 0 : o1.size()).compareTo((o2 == null) ? 0 : o2.size());
}
};
public static void main(String[] args) {
// LinkedList<LinkedList<Long>>() overalllList = new LinkedList<LinkedList<Long>();
// Note there is an extra() to the left of your overalllList.
LinkedList<LinkedList<Long>> overalllList = new LinkedList<LinkedList<Long>>();
LinkedList<Long> list3 = new LinkedList<Long>();
LinkedList<Long> list2 = new LinkedList<Long>();
LinkedList<Long> list1 = new LinkedList<Long>();
for (long i = 0; i < 5; i++) { // 5, or 1000
if (i < 2) {
list1.add(i);
}
if (i < 3) { // 3, or 245.
list2.add(i);
}
list3.add(i);
}
overalllList.add(list3);
overalllList.add(list2);
overalllList.add(list1);
System.out.println("Before: " + overalllList);
Collections.sort(overalllList, comp);
System.out.println("After: " + overalllList);
}
輸出是
Before: [[0, 1, 2, 3, 4], [0, 1, 2], [0, 1]]
After: [[0, 1], [0, 1, 2], [0, 1, 2, 3, 4]]
是的,你需要自定義的'Comparator'。 –
您將需要考慮您的列表將具有可變元素。所以它可能不會一次訂購。 –
謝謝你讓我知道,我現在就去看看。 – Tony