Java按大小顺序对LinkedList的LinkedList进行排序

Java按大小顺序对LinkedList的LinkedList进行排序,java,list,collections,comparator,Java,List,Collections,Comparator,我目前面临这个问题:我有一个LinkedList,其中包含多个包含long的LinkedList,因此: LinkedList<LinkedList<Long>>() overalllList = new LinkedList<LinkedList<Long>(); LinkedList()overallist=new LinkedList下面是一个方法示例 // A "size()" comparator private static Compar

我目前面临这个问题:我有一个LinkedList,其中包含多个包含long的LinkedList,因此:

LinkedList<LinkedList<Long>>() overalllList = new LinkedList<LinkedList<Long>();

LinkedList()overallist=new LinkedList下面是一个方法示例

// 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);
}
List-overallist=new-LinkedList();
add(Arrays.asList(1L、2L、3L));
add(Arrays.asList(4L、5L、6L、7L、8L));
add(Arrays.asList(9L));
Collections.sort(全局,新的Comparator(){
@凌驾
公共整数比较(列表1、列表2){
返回list1.size()-list2.size();
}
});

<代码>是的,您需要自定义<代码>比较器< /代码>。您需要考虑您的列表将具有可变元素。所以它可能不会在某一点上被订购。谢谢你让我知道,我现在来看看。谢谢Elliott,我会试试的!
// 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]]
List<List<Long>> overalllList = new LinkedList<List<Long>>();
overalllList.add(Arrays.asList(1L, 2L, 3L));
overalllList.add(Arrays.asList(4L, 5L, 6L, 7L, 8L));
overalllList.add(Arrays.asList(9L));

Collections.sort(overalllList, new Comparator<List<Long>>() {
    @Override
    public int compare(List<Long> list1, List<Long> list2) {
        return list1.size() - list2.size();
    }
});