简体   繁体   English

如何按列值对 2D ArrayList 进行排序,如果值相等怎么办?

[英]How to sort a 2D ArrayList by column value, and what if the values are equal?

I want to sort two-dimensional ArrayList by their specific column values and if the column values of two arrays are equal then compare the two arrays using another column value.我想通过它们的特定列值对二维ArrayList进行排序,如果两个 arrays 的列值相等,则使用另一个列值比较两个 arrays。

For example:例如:

array = [[2, 3, 4, 5][0, 2, 3, 4][0, 1, 2, 3][1, 2, 3, 4]]

I want to compare them first with their index [0] , but since array[1][0] and array[2][0] have the same value 0 I want to compare these two arrays by their index [3] .我想先将它们与它们的索引[0]进行比较,但由于array[1][0]array[2][0]具有相同的值0我想通过它们的索引[3]来比较这两个 arrays 。

So, the new array would be:因此,新数组将是:

[[0, 1, 2, 3][0, 2, 3, 4][1, 2, 3, 4][2, 3, 4, 5]]

I have this code我有这个代码

public ArrayList<List<Integer>> sortArray(ArrayList<List<Integer>> spaces) {
    final Comparator<List<Integer>> comparator = new Comparator<List<Integer>>() {
        @Override
        public int compare(List<Integer> sList1, List<Integer> sList2) {
            return sList1.get(0).compareTo(sList2.get(0));
        }
    };

    Collections.sort(spaces, comparator);

    return spaces;
}

However, this only compares the value by the index [0] , and no condition when the values are equal.但是,这仅通过索引[0]比较值,并且当值相等时没有条件。

I think you can use the Java Collections.List's compareTo method (Let's abbreviate CT).我想你可以使用 Java Collections.List 的 compareTo 方法(我们简称 CT)。 Here are some example outputs of CT.以下是 CT 的一些示例输出。 5.CT(7) -> -1; 5.CT(7)->-1; 7.CT(5) -> 1; 7.CT(5) -> 1; 5.CT(5) -> 0; 5.CT(5)->0; compareTo returns 0 when they're the same, so you simply don't swap their positions.当它们相同时 compareTo 返回 0,所以你根本不交换它们的位置。 I'm not sure if I'm answering the question correctly though.我不确定我是否正确回答了这个问题。

You need to compare all items in sub lists.您需要比较子列表中的所有项目。 For example:例如:

public int compare(List<Integer> sList1, List<Integer> sList2) {
    for (int i = 0; i < 4; i++) {
        int c = sList1.get(i).compareTo(sList2.get(i));
        if (c != 0)
            return c;
    }
    return 0;
}

Not suggested if you are looking for a fast algorithm.如果您正在寻找快速算法,则不建议使用。

You need to modify your Comparator to handle that index check something like this:您需要修改Comparator以处理该索引检查,如下所示:

public int compare(List<Integer> sList1, List<Integer> sList2) {
    if (sList1.get(0) != sList2.get(0)) {
        return sList1.get(0).compareTo(sList2.get(0));
    } else {
        return sList1.get(3).compareTo(sList2.get(3));
    }
}

But if you are running Java 8 and above you can directly do something like this to sort your collection:但是,如果您正在运行 Java 8 及更高版本,您可以直接执行以下操作来对您的集合进行排序:

Collections.sort(spaces, (sList1, sList2) -> {
    if (sList1.get(0) != sList2.get(0)) {
        return sList1.get(0).compareTo(sList2.get(0));
    } else {
        return sList1.get(3).compareTo(sList2.get(3));
    }
});

For pre-Java 8 you can do similar to what you are already doing:对于 Java 8 之前的版本,您可以执行类似于您已经在执行的操作:

Collections.sort(spaces, new Comparator<List<Integer>>() {
    @Override
    public int compare(List<Integer> sList1, List<Integer> sList2) {
        if (sList1.get(0) != sList2.get(0)) {
            return sList1.get(0).compareTo(sList2.get(0));
        } else {
            return sList1.get(3).compareTo(sList2.get(3));
        }
    }
});

In both cases, the output will be:在这两种情况下,output 将是:

[[0, 1, 2, 3], [0, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5]]

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

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