排序整数arraylist的arrylist不依赖于它们在java中的长度

时间:2021-01-19 21:20:42

i want to sort ArrayList of Arraylist of Integer, and their lengths are different
i try the codes here sort an arraylist of arraylist of integers it's worked but its priority in order of lengths first not values

我想对整数的Arraylist的ArrayList进行排序,并且它们的长度是不同的我在这里尝试代码排序整数的arraylist的arraylist它的工作但它的优先级按长度顺序首先不是值


for example:
2, 2 , 2, 3
2, 2, 3, 4
2, 2, 90
its output will be like this:
2, 2, 90
2, 2, 2, 3
2, 2, 3, 4
While the order I want:
2, 2, 2, 3
2, 2, 3, 4
2, 2, 90
can anyone help me to solve it?

例如:2,2,2,3,2,2,3,4 2,2,9,它的输出将是这样的:2,2,9 2,2 2,2,3 2,3 2,3,4我想要的顺序:2,2,2,3,2,2,3,4,2,90可以帮助我解决吗?

2 个解决方案

#1


2  

The code you are looking at only compares the first number in each list, so the order they end up in isn't actually by size; it's arbitrary.

您正在查看的代码仅比较每个列表中的第一个数字,因此它们最终的顺序实际上并不是按大小排列的;这是武断的。

The key is this part

关键是这一部分

public int compare(List<Integer> o1, List<Integer> o2) {
    return o1.get(0).compareTo(o2.get(0));
}

You need something like

你需要类似的东西

public int compare(List<Integer> o1, List<Integer> o2) {
    for (int i = 0; i < o1.size(); i++) {
        if (o1.get(i).compareTo(o2.get(i)) != 0) {
            return o1.get(i).compareTo(o2.get(i));
        }
    }
}

I have left this incomplete since I assume this is supposed to be educational for you and there's nothing to be accomplished if I give you the whole answer. The code snipped I've written here will only properly compare lists of the same length. If the second list is longer extra values will be ignored and if the first list is longer it will error out. I will leave it as an exercise to you to resolve those issues. :-)

因为我认为这应该对你很有教育,如果我给你完整的答案,那就没有什么可以完成的了。我在这里写的代码剪切只会正确比较相同长度的列表。如果第二个列表较长,则会忽略额外值,如果第一个列表较长,则会出错。我将把它留给你来解决这些问题。 :-)

#2


0  

You can do it in two steps: first, to sort arrays from the min to max values with the help of forEach(Collections::sort) method and, second, then make a comparison of the arrays and arrange them in specific order:

您可以分两步完成:首先,在forEach(Collections :: sort)方法的帮助下,将数组从最小值排序到最大值,然后,对数组进行比较并按特定顺序排列:

    List<List<Integer>> listOLists = Arrays.asList(
            Arrays.asList(2, 90, 2),
            Arrays.asList(3, 2, 4, 2),
            Arrays.asList(2, 2, 3, 2)); 

//first step:   
    listOLists.forEach(Collections::sort);

//second step:
    Collections.sort(listOLists, (l1, l2) -> {
        for (int i = 0; i < listOLists.size(); i++) {
        if (l1.get(i) != l2.get(i)) {
            return l1.get(i) - l2.get(i);}
        }
        return l1.get(0).compareTo(l2.get(0));
        });
    System.out.println(listOLists);

As a result you'll get:

结果你会得到:

[[2, 2, 2, 3], [2, 2, 3, 4], [2, 2, 90]]

#1


2  

The code you are looking at only compares the first number in each list, so the order they end up in isn't actually by size; it's arbitrary.

您正在查看的代码仅比较每个列表中的第一个数字,因此它们最终的顺序实际上并不是按大小排列的;这是武断的。

The key is this part

关键是这一部分

public int compare(List<Integer> o1, List<Integer> o2) {
    return o1.get(0).compareTo(o2.get(0));
}

You need something like

你需要类似的东西

public int compare(List<Integer> o1, List<Integer> o2) {
    for (int i = 0; i < o1.size(); i++) {
        if (o1.get(i).compareTo(o2.get(i)) != 0) {
            return o1.get(i).compareTo(o2.get(i));
        }
    }
}

I have left this incomplete since I assume this is supposed to be educational for you and there's nothing to be accomplished if I give you the whole answer. The code snipped I've written here will only properly compare lists of the same length. If the second list is longer extra values will be ignored and if the first list is longer it will error out. I will leave it as an exercise to you to resolve those issues. :-)

因为我认为这应该对你很有教育,如果我给你完整的答案,那就没有什么可以完成的了。我在这里写的代码剪切只会正确比较相同长度的列表。如果第二个列表较长,则会忽略额外值,如果第一个列表较长,则会出错。我将把它留给你来解决这些问题。 :-)

#2


0  

You can do it in two steps: first, to sort arrays from the min to max values with the help of forEach(Collections::sort) method and, second, then make a comparison of the arrays and arrange them in specific order:

您可以分两步完成:首先,在forEach(Collections :: sort)方法的帮助下,将数组从最小值排序到最大值,然后,对数组进行比较并按特定顺序排列:

    List<List<Integer>> listOLists = Arrays.asList(
            Arrays.asList(2, 90, 2),
            Arrays.asList(3, 2, 4, 2),
            Arrays.asList(2, 2, 3, 2)); 

//first step:   
    listOLists.forEach(Collections::sort);

//second step:
    Collections.sort(listOLists, (l1, l2) -> {
        for (int i = 0; i < listOLists.size(); i++) {
        if (l1.get(i) != l2.get(i)) {
            return l1.get(i) - l2.get(i);}
        }
        return l1.get(0).compareTo(l2.get(0));
        });
    System.out.println(listOLists);

As a result you'll get:

结果你会得到:

[[2, 2, 2, 3], [2, 2, 3, 4], [2, 2, 90]]