如何在Java中使用Comparator对二维数组进行排序。

huangapple go评论72阅读模式
英文:

How to sort two dimensional array using Comparator in java

问题

int[][] arr = {{1, 2, 3}, {}, {3}, {1, 4}, {3, 2}, {3, 3, 5}};

Arrays.sort(arr, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        if(o1[0] == o2[0])
            return o1[1] - o2[1];
        return o2[0] - o1[0];
    }
});

for (int[] ints : arr) {
    for (int anInt : ints) {
        System.out.print(anInt + " ");
    }
    System.out.println();
}
英文:

I need to sort such an array by descending the elements of the first column. In the case of equality of elements in the first column, the elements of the second column must be sorted in ascending order. But this what I need else is to check and put empty row to the end of matrix, and row with only one element put prior to the same which has more elements.
For example in this array {3} - it is the first row, {} - the last one.

int[][] arr = {{1, 2, 3}, {}, {3}, {1, 4}, {3, 2}, {3, 3, 5}};

 Arrays.sort(arr, new Comparator&lt;int[]&gt;() {
     @Override
     public int compare(int[] o1, int[] o2) {
        if(o1[0] == o2[0])
            return o1[1] - o2[1];
        return o2[0] - o1[0];
    }
});

for (int[] ints : arr) {
    for (int anInt : ints) {
        System.out.print(anInt + &quot; &quot;);
    }
    System.out.println();
}

答案1

得分: 4

以下Comparator<int[]>可用于排序:

  1. 将空数组放在最后。

  2. 按升序排列相同索引处的较大数字。

  3. 如果较小的数组在索引0处首先出现在较大的数组之前(按长度来衡量),则将较小的数组视为比较靠前。

以下是一个根据您的需要轻松修改的算法:

int[][] arr = {{1, 2, 3}, {}, {3}, {1, 4}, {3, 2, 2}, {3, 3, 5}, {3, 2}};

Arrays.sort(arr, (o1, o2) -> {
    if (o1.length == 0) { return 1; }         // 空数组排在最后
    if (o2.length == 0) { return -1; }        // 空数组排在最后
    int min = Math.min(o1.length, o2.length); // 避免ArrayIndexOutOfBoundsException的上限
    for (int i = 0; i < min ; i++) {
        if (o1[i] != o2[i]) {                 // 比较索引上的值
            return o1[i] - o2[i];             // 如果不同,则返回差值
        } 
    }
    return 1;                                 // o1靠前,因此它排在前面
});

System.out.println(Arrays.deepToString(arr)); 

输出将是:

[[1, 2, 3], [1, 4], [3], [3, 2, 2], [3, 2], [3, 3, 5], []]
英文:

The following Comparator&lt;int[]&gt; enables sorting by:

  1. Empty arrays as last

  2. Bigger number at the same index in the ascending order

  3. In case the smaller array first into larger (in terms of length) starting at index 0, which one comes first is considered smaller compared to the latter one.

Here is an algorithm can be easily modified according to your needs:

int[][] arr = {{1, 2, 3}, {}, {3}, {1, 4}, {3, 2, 2}, {3, 3, 5}, {3, 2}};

Arrays.sort(arr, (o1, o2) -&gt; {
		if (o1.length == 0) { return 1; }         // empty last
		if (o2.length == 0) { return -1; }        // empty last
		int min = Math.min(o1.length, o2.length); // upper bound to avoid ArrayIndexOutOfBoundsException
		for (int i = 0; i &lt; min ; i++) {
			if (o1[i] != o2[i]) {                 // compare values on indices
                return o1[i] - o2[i];             // return if different
            } 
		}
		return 1;                                 // it goes first so it lefts first
	}
);

System.out.println(Arrays.deepToString(arr)); 

The output will be:

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

huangapple
  • 本文由 发表于 2020年4月8日 03:40:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61088123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定