对二维数组进行排序

发布于 2024-10-02 03:45:13 字数 200 浏览 0 评论 0原文

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

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

将被分类为:

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

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独行侠 2024-10-09 03:45:13

试试这个:

    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];
        }
    });

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

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.

○闲身 2024-10-09 03:45:13
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];
    }
});
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];
    }
});
鹿港巷口少年归 2024-10-09 03:45:13

它只不过是基数排序。它的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;
 }
}

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

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

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.

临风闻羌笛 2024-10-09 03:45:13

我不能具体谈论java,但算法应该是可翻译的。要点是在交换时移动该行的两个(或多个)元素。

int var[ n ][ 2 ] // 你的 int 数组
// [[选择排序方法]]
// 我将使用冒泡排序
// 为了清楚起见,尽管效率低下
int 温度[ 2 ];
布尔仍然排序 = true;

{

<块引用>

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

<块引用>

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

<块引用>

临时[ 0 ] = var[ x ][ 0 ]; // 如果大于 2
温度[ 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 ];
仍然排序 = true;
}
}
}
while( 仍在排序 );



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.

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 );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文