如何对简单的整数二维数组排序? [英] How do i sort a simple integer 2 dimensional array?

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

问题描述

我有一个二维数组,其维度为 myArray [x] [3] .我需要基于 [x] [0] 对数组进行排序.我正在使用 Arrays.sort(myArray); .那是可行的,但是当时的数组是 myArray [x] 的一维数组.然后我改变了主意,将其更改为二维数组.它由1到9的整数填充.我已经搜索了将2维数组排序的clear方法,但找不到简单的解释.请帮忙.

I have a 2 dimensional array, that has dimensions of myArray[x][3]. I need to sort the array based upon [x][0]. I was using Arrays.sort(myArray);. That was working, however, the array at the time was a one dimension array of myArray[x]. Then I changed my mind and changed it into a 2 dimensional array. It is filled with integers from 1 to 9. I have searched for the clear method to sort 2 dimensional arrays, and cannot find the simple explanations. Please help.

谢谢;冰

好,这是代码:

public static void sortArray(int myArray[][]){
Arrays.sort(myArray, new Comparator<Integer[]>(){
    @Override
    public int compare(Integer[] o1, Integer[] o2) {
        return o1[0].compareTo(o2[0]);
    }
});

那行吗?

好的,这是问题所在.排序后的数组开始时是未排序的,就像这样:

OK, here is the problem. The sorted array starts out unsorted, like this:

3 - 0 - 0
4 - 0 - 1
5 - 0 - 2
6 - 0 - 3
3 - 0 - 4

第一列 [0] [x] 是值,第二列 [1] [x] 是数组字段计数,最后一列 [2] [x] 是数组中的实际列号.整体方法是从原始二维数组中提取整行,然后将其加载到x宽3高的数组中,然后根据 [0] [x] 列对数组进行排序.这是排序函数现在被调用后的结果:

The first column [0][x] is the value, the second column [1][x] is the array field count, and the last column [2][x] is the actual column number in the array. The overall method, takes a entire row from the original 2 dimensional array, and loads it into a 3-tall by x-wide array, then sorts the array based on the [0][x] column. Here is the result after the sort function now being called:

0 - 0 - 3
0 - 1 - 4
0 - 2 - 5
0 - 3 - 6
0 - 4 - 3

以某种方式,我复制并粘贴的方法换出了数字,似乎排序是错误的.两个输出都使用相同的 System.out.print .

Somehow, the method I copied and pasted, is swapping out the numbers, seems like the sorting is wrong. Same System.out.print is being used on both outputs.

推荐答案

如果我做对了:

    Integer[][] numbers = new Integer[][]{{7, 8, 9}, {1, 2, 3}};
    System.out.println("Before:");
    for(Integer[] row : numbers) {
        for(Integer num : row) {
            System.out.print(num);
        }
        System.out.println("");
    }

    Arrays.sort(numbers, new Comparator<Integer[]>(){
        @Override
        public int compare(Integer[] o1, Integer[] o2) {
            return o1[0].compareTo(o2[0]);
        }
    });
    System.out.println("After:");
    for(Integer[] row : numbers) {
        for(Integer num : row) {
            System.out.print(num);
        }
        System.out.println("");
    }

打印:


Before:
789
123
After:
123
789

更新:

这正是您所需要的.

public static void sortArray(int myArray[][]) {
    Arrays.sort(myArray, new Comparator<int[]>() {

        @Override
        public int compare(int[] o1, int[] o2) {
            return Integer.valueOf(o1[0]).compareTo(Integer.valueOf(o2[0]));
        }

    });
}

Update2:

对每一行进行排序:

public static void sortEachRow(int myArray[][]) {
    for(int[] row : myArray) {
        Arrays.sort(row);
    }
}

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

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