简体   繁体   English

使用 Arrays.sort 按最后一行对二维数组进行排序

[英]Sorting 2d-array by last row with Arrays.sort

Is it possible to sort 2d-array by last row with Arrays.sort(,) in Java.是否可以使用 Java 中的 Arrays.sort(,) 按最后一行对二维数组进行排序。 The following snippet works great for sorting by last column but it doesn't seem to have a way to be adjusted for sorting by last row.以下代码段非常适合按最后一列排序,但似乎没有办法调整按最后一行排序。

My first thought was to use tranforming columns to rows, doing sort and then transforming rows to column.我的第一个想法是使用将列转换为行,进行排序,然后将行转换为列。 Any better way for very big arrays?对于非常大的 arrays 有什么更好的方法吗?

int[][] twoDim = { {1, 2, 3}, {3, 7, 11}, {8, 9, 16}, {4, 2,8}, {5, 3, 9} };
Arrays.sort(twoDim, new Comparator<int[]>() {
     @Override
     public int compare(int[] o1, int[] o2) {
         return ((Integer) o1[2]).compareTo(o2[2]);
     }
});

Let's elaborate the whole situation: This is where I am when my array gets initialized and by rows and columns you can imagine this dataset as following:让我们详细说明一下整个情况:这就是我的数组被初始化的地方,你可以通过行和列来想象这个数据集如下:

{1, 2, 3}, //first row with three columns
{3, 7, 11}, //second row with three columns
{8, 9, 16},
{4, 2, 8},
{5, 3, 9} //last row with three columns

Sorting by last row means to rearrange the position of first and second column because 5 is bigger than 3. So after rearranging dataset it looks like:按最后一行排序意味着重新排列第一列和第二列的 position,因为 5 大于 3。所以在重新排列数据集后它看起来像:
2, 1, 3
7, 3, 11
9, 8, 16
2, 4, 8
3, 5, 9 //now it's ordered by last row (first and second column have changed they position, by chance third column is in a right place already)

This cannot be answered if I understand what you mean by columns and rows correctly.如果我正确理解列和行的含义,则无法回答此问题。

If you look at the dataset like this:如果你看这样的数据集:

1, 2, 3
3, 7, 11
8, 9, 16
4, 2, 8
5, 3, 9

Now, if you sort these by the last row, you get these results:现在,如果您按最后一行对它们进行排序,您会得到以下结果:

{2, 7, 9, 2, 3}, {1,3,8,4,5}, {3, 11, 16, 8, 9}

This obviously will not be the case if you replace the 4, 2, 8 row with the 5,3,9 row.如果将 4、2、8 行替换为5,3,94, 2, 8显然不会出现这种情况。 So, you have to either come up with a standard ordering, or find a different way to solve the your actual problem that you're facing.因此,您必须要么提出标准排序,要么找到不同的方法来解决您面临的实际问题。

If you are dealing with matrices, I would highly recommend a library .如果您正在处理矩阵,我强烈推荐一个library

Interesting question.有趣的问题。

I would do it by implementing a variation of quick sort .我会通过实现快速排序的变体来做到这一点。 The variations are essentially in the partition function:变化基本上在partition function 中:

  • you use the last row of your matrix as the array to be sorted您使用矩阵的最后一行作为要排序的数组
  • when swapping two elements, you actually swap two columns of your matrix交换两个元素时,实际上交换矩阵的两列

Here's an implementation:这是一个实现:

public void qsortOnLastRow(int[][] matrix, int left, int right) {
    if (left < right) {
        int i = partition(matrix, left, right);
        qsortOnLastRow(matrix, left, i - 1);
        qsortOnLastRow(matrix, i + 1, right);
    }
}

public int partition(int[][] matrix, int left, int right) {
    int lastrow = matrix.length - 1;
    int pivotValue = matrix[lastrow][left];
    int i = left;
    for (int j = left + 1; j <= right; j++) {
        if (matrix[lastrow][j] <= pivotValue) {
            i++;
            swapColumns(matrix, i, j);
        }
    }
    swapColumns(matrix, left, i);
    return i;
}

public void swapColumns(int[][] matrix, int c0, int c1) {
    if (c0 != c1) {
        for (int i = 0; i < matrix.length; i++) {
            int t = matrix[i][c0];
            matrix[i][c0] = matrix[i][c1];
            matrix[i][c1] = t;
        }
    }
}

You can sort your int[][] matrix by calling qsortOnLastRow(matrix, 0, matrix[0].length - 1) ;您可以通过调用qsortOnLastRow(matrix, 0, matrix[0].length - 1)对您的int[][] matrix进行排序;

Complexity, if I'm not wrong, should be O(m * n * log n) , where m = number of rows and n = number of columns in your matrix.如果我没记错的话,复杂性应该是O(m * n * log n) ,其中 m = 行数, n = 矩阵中的列数。

Note : You can use the same trick (sorting on the last row and swapping columns) also with other sorting algorithms.注意:您也可以将相同的技巧(在最后一行排序并交换列)与其他排序算法一起使用。

Remember that two dimensional arrays are arrays of arrays.请记住,二维 arrays 是 arrays 的 arrays。 Every sort algorithm needs a facility to move the entries you want to sort.每个排序算法都需要一个工具来移动要排序的条目。 Your solution to sort by last column works, because Arrays.sort sees your inner arrays as the objects to sort.您按最后一列排序的解决方案有效,因为Arrays.sort将您的内部 arrays 视为要排序的对象。 What you call sorting by last row, has no equivalent object, which should represent columns.您所谓的按最后一行排序,没有等效的 object,它应该代表列。

So you have two options:所以你有两个选择:

  1. Implement your own sorting algorithm which swaps whole columns at once, but please use a text book algorithm.实现您自己的排序算法,一次交换整个列,但请使用教科书算法。

  2. Transpose your matrix.转置你的矩阵。 But remember that this may be free, if it is possible to swap the meaning of the first and second index in the whole program.但是请记住,如果可以在整个程序中交换第一个和第二个索引的含义,这可能是免费的。

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

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