排序二维数组 [英] Sorting a 2 dimensional array

查看:70
本文介绍了排序二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]

将被分类为:

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

谢谢.

推荐答案

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

这篇关于排序二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆