java 对二维数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4158679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:06:24  来源:igfitidea点击:

Sorting a 2 dimensional array

javasortingmultidimensional-array

提问by siame

I've got a 2D array that I'd like to sort into descending order depending on the contents of the first column, however I'd like the array to retain each row and move the second column as the first moves. To put it into an example;

我有一个二维数组,我想根据第一列的内容按降序排序,但是我希望数组保留每一行并在第一次移动时移动第二列。把它放在一个例子中;

[2, 5]
[4, 18]
[1, 7]
[9, 3]

would be sorted into:

将被分类为:

[9, 3]
[4, 18]
[2, 5]
[1, 7]

Thanks.

谢谢。

采纳答案by Bj?rn

int[][] d2 = {
           {2,5},
           {4,18},
           {1,7},
           {9,3}
          };

java.util.Arrays.sort(d2, new java.util.Comparator<int[]>() {
    public int compare(int[] a, int[] b) {
        return b[0] - a[0];
    }
});

回答by Amir Raminfar

Try this:

试试这个:

    int[][] test = new int[][]{{2,5}, {4,18}, {1,7},{9,3}};
    Arrays.sort(test, new Comparator<int[]>() {
        @Override
        public int compare(int[] o1, int[] o2) {
            return o2[0] - o1[0];
        }
    });

I haven't tested this but it should work. Note you may want to reverse the subtraction to change descending.

我还没有测试过这个,但它应该可以工作。请注意,您可能希望反转减法以更改降序。

回答by itsme

It's nothing but Radix Sort. Its C code is as follows:

它只不过是基数排序。其C代码如下:

void Rsort(int *a, int n)
{
  int i, b[MAX], m = a[0], exp = 1;
  for (i = 0; i < n; i++)
  {
    if (a[i] > m)
      m = a[i];
  }

  while (m / exp > 0)
  {
    int bucket[10] =
    {  0 };
    for (i = 0; i < n; i++)
      bucket[a[i] / exp % 10]++;
    for (i = 1; i < 10; i++)
      bucket[i] += bucket[i - 1];
    for (i = n - 1; i >= 0; i--)
      b[--bucket[a[i] / exp % 10]] = a[i];
    for (i = 0; i < n; i++)
      a[i] = b[i];
    exp *= 10;
 }
}

Here it's operating on digits of the numbers in array. It's not much harder to edit the code to get code for the above problem. Here each element of the array is considered as a digit for that ROW NUMBER.

这里它对数组中数字的数字进行操作。编辑代码以获取上述问题的代码并不难。这里数组的每个元素都被视为该 ROW NUMBER 的一个数字。

回答by NMPrado

I can't speak to java specifically but the algorithm should be translatable. The point is to move both elements (or more) of the row when swapping.

我不能专门与 java 交谈,但算法应该是可翻译的。关键是在交换时移动行的两个(或更多)元素。

int var[ n ][ 2 ] // your int array
// [[ choose a sort method ]]
// I'm going to use a bubble sort
// for clarity, despite inefficiency
int temp[ 2 ];
bool stillSorting = true;
do
{

stillSorting = false;
for ( int x = n; x < 1; x-- )
{

if ( var[ x ][ 0 ] > var[ x-1 ][ 0 ] )
{

temp[ 0 ] = var[ x ][ 0 ]; // if it's more than 2
temp[ 1 ] = var[ x ][ 1 ]; // consider using a loop
var[ x ][ 0 ] = var[ x-1 ][ 0 ];
var[ x ][ 1 ] = var[ x-1 ][ 1 ];
var[ x-1 ][ 0 ] = temp[ 0 ];
var[ x-1 ][ 1 ] = temp[ 1 ];
stillSorting = true;
}
}
}
while( stillSorting );

int var[ n ][ 2 ] // 您的 int 数组
// [[ 选择排序方法 ]] // 为了清晰起见,
我将使用冒泡排序
// 尽管效率低下
int temp[ 2 ];
bool stillSorting = true;

{

仍然排序 = 假;
for ( int x = n; x < 1; x-- )
{

if ( var[ x ][ 0 ] > var[ x-1 ][ 0 ] )
{

temp[ 0 ] = var[ x ][ 0 ]; // 如果超过 2
temp[ 1 ] = var[ x ][ 1 ]; // 考虑使用循环
var[ x ][ 0 ] = var[ x-1 ][ 0 ];
var[ x ][ 1 ] = var[ x-1 ][ 1 ];
var[ x-1 ][ 0 ] = temp[ 0 ];
var[ x-1 ][ 1 ] = temp[ 1 ];
仍然排序 = 真;
}
}
}
而(stillSorting);