简体   繁体   English

Java 中的列表排序问题

[英]List within list Sorting issue in Java

I am trying to sort a List<List<Integer>> .我正在尝试对List<List<Integer>>进行排序。 The internal list will have 3 values.内部列表将有 3 个值。 (X-coordinate, Y-coordinate, Start/end) Now while it works for most of the cases, the sorting fails for a specific case. (X 坐标、Y 坐标、开始/结束)现在虽然它适用于大多数情况,但对于特定情况,排序会失败。

The code I used for sorting is:我用于排序的代码是:

arr.sort((a,b) -> {
            if(a.get(0)!=b.get(0)){
                return a.get(0) - b.get(0);
            }
            else{
                return  (a.get(2)==0?-a.get(1):a.get(1)) - (b.get(2)==0?-b.get(1):b.get(1));
            }
        });

The logic behind it is: If x value is not equal we sort them on the basis of x.其背后的逻辑是:如果 x 值不相等,我们将根据 x 对它们进行排序。 if equal then: if both start then on basis of height in descending order.如果相等,则:如果两者都以降序开始,则基于高度。 if both end then on basis of height in ascending order.如果两者都以升序结束,则以高度为基础。 else whichever was the start.否则以开始为准。

The start is shown as 0 and end as 1.开始显示为 0,结束显示为 1。

The example in which is failing is: arr:失败的例子是:arr:

[[1, 10000, 0], [10001, 10000, 1], [2, 9999, 0], [10001, 9999, 1]]

Output: Output:

[[1, 10000, 0], [2, 9999, 0], [10001, 10000, 1], [10001, 9999, 1]]

while the expected sorting is:而预期的排序是:

[[1, 10000, 0], [2, 9999, 0], [10001, 9999, 1], [10001, 10000, 1]]

Can you please help me find out what I am doing wrong?你能帮我找出我做错了什么吗?

The code is working as intended.代码按预期工作。 Since in your code for the comparison between [10001, 9999, 1] and [10001, 10000, 1] , it will go in the else statement and since a.get(2) and b.get(2) both are non zero, the else statement will be reduced to b.get(1) - a.get(1) , which sorts in descending order.由于在[10001, 9999, 1][10001, 10000, 1]之间进行比较的代码中,它将在else语句中为 go 并且因为a.get(2)b.get(2)都不是零, else语句将简化为b.get(1) - a.get(1) ,按降序排列。 Therefore, it will sort in the order [10001, 10000, 1] and then [10001, 9999, 1] .因此,它将按[10001, 10000, 1]的顺序排序,然后是[10001, 9999, 1]

Also, boxed integers ( Integer ) should not be compared using == or != operators as they will be compared by reference.此外,不应使用==!=运算符比较盒装整数 ( Integer ),因为它们将通过引用进行比较。 Instead, equals method should be used.相反,应该使用equals方法。

The second else condition should subtract y of b from y of a.第二个 else 条件应该从 a 的 y 中减去 b 的 y。 Something like this:像这样的东西:

(a,b) -> {
        if(!a.get(0).equals(b.get(0))){
            return a.get(0) - b.get(0);
        }
        else{
            return (a.get(2).equals(0)?-a.get(1):a.get(1)) - (b.get(2).equals(0)?-b.get(1):b.get(1));
        }
    }

I think the original sort code is very hard to read and don't match your expectation.我认为原始排序代码很难阅读并且不符合您的期望。 I change it to:我将其更改为:

public static void main(String[] args) {
        List<List<Integer>> list = Arrays.asList(Arrays.asList(1, 10000, 0),
                Arrays.asList(10001, 10000, 1), Arrays.asList(2, 9999, 0)
        ,Arrays.asList(10001, 9999, 1)
        , Arrays.asList(10001, 9999, 0));

        Collections.sort(list, (a,b) -> {
            if(!a.get(0).equals(b.get(0))){
                return a.get(0) - b.get(0);
            }
            else{
                if (b.get(2).equals(a.get(2))) {
                    if (a.get(2) == 0) {
                        return Integer.compare(b.get(1), a.get(1)); //start, descending
                    } else {
                        return Integer.compare(a.get(1), b.get(1)); // ascending
                    }
                } else {
                    return  a.get(2) == 0 ? -1 : 1; // 0 first
                }
            }
        });

        System.out.println(list); // [[1, 10000, 0], [2, 9999, 0], [10001, 9999, 0], [10001, 9999, 1], [10001, 10000, 1]]

    }

Hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM