排序基于列的二维整数数组 [英] Sorting a 2D Integer array based on a column

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

问题描述

我有一个二维阵列,我想基于第二列进行排序。第一列应与第二列保持成对

I have a 2D-array that I want to sort based on the second column. The first column should remain paired with the second column.

二维数组最初如下(2×10矩阵):

The 2D-array is initially as follows (2x10 matrix):

0 10
1 9
2 9
3 9
4 15
5 10
6 4
7 8
8 11
9 12

我想上面的二维数组排序是这样的:

I want the above 2D-array to be sorted like this:

4 15
9 12
8 11
0 10
5 10
1 9
2 9
3 9
7 8
6 4

现在,我已经尝试从适应的回答:<一href=\"http://stackoverflow.com/questions/4907683/sort-a-two-dimensional-array-based-on-one-column\">Sort根据一列的进入这个code进行二维阵列

Now, I've tried adapting the answer from: Sort a two dimensional array based on one column into this code:

Arrays.sort(theArray, new Comparator<Integer[]>()
{
    @Override
    public int compare(Integer[] int1, Integer[] int2)
    {
        Integer numOfKeys1 = int1[1];
        Integer numOfKeys2 = int2[1];
        return numOfKeys1.compareTo(numOfKeys2);
    }
});

但是,它似乎并没有数组在所有排序。当打印数组调用sort后()函数数组是在其最初的顺序。

However, it doesn't seem to sort the array at all. When printing the array after calling the sort() function the array is in its initial order.

我也试过从这里适应了答案:分拣字符串二维数组java的但我遇到同样的问题。

I also tried adapting the answer from here: sorting 2D array of String in java but I encountered the same problem.

有适应这些解决方案时,我做了一些致命的错误,还是应该我的code的工作?

Have I made some fatal mistake when adapting these solutions, or should my code work?

另外,我将如何去选这个数组按降序排列?我将取代return语句中比较()这一行?

Also, how would I go about sorting this array in descending order? Would I replace the return statement in compare() with this line?

return -numOfKeys2.compareTo(numOfKeys1);

任何帮助将大大AP preciated。谢谢!

Any help would be greatly appreciated. Thanks!

编辑:刚刚发布我的code的剩余部分,​​看问题是否在其他地方

Just posting the rest of my code to see if the problem is elsewhere.

public void Sort()
{   
    Integer[][] theArray = {{0,10},{1,9},{2,9},{3,9},{4,15},{5,10},{6,4},{7,8},{8,11},{9,12}};;

    dump(theArray);
    Arrays.sort(theArray, new Comparator<Integer[]>()
    {
        @Override
        public int compare(Integer[] int1, Integer[] int2)
        {
            Integer numOfKeys1 = int1[1];
            Integer numOfKeys2 = int2[1];
            return numOfKeys1.compareTo(numOfKeys2);
        }
    });

    System.out.println("====");
    dump(theArray);     
}

public void dump(Integer[][] array)
{
    for(int p = 0, q = 10; p < q; p++)
    {
        System.out.println(array[p][0] + " " + array[p][1]);
    }
}

编辑2:

我找到了工作。谢谢大家对你的帮助。我曾多次排序()函数(一老一,这不是工作,你在上面看到的),而且事实证明我打电话错了,虽然我还以为我换了电话。只是那些日子之一。

I've got it working. Thanks everyone for your help. I had multiple Sort() functions (an older one that wasn't working, and the one you see above), and it turns out I was calling the wrong one, even though I thought I changed the call. Just one of those days.

随意使用code。如果要排序的数组以上。它现在完全正常工作。

Feel free to use the code above if you want to sort an array. It's fully working now.

推荐答案

它工作正常的我。为了扭转为了你否定原来的的compareTo 的交换变量,但不能同时使用。

It works fine for me. To reverse order you'd negate the original compareTo, or swap the variables, but not both.

我们可能会需要看到code其余理解为什么你看到,你看到的;我剪切和粘贴您的code一字不差,那么赔率是很好的问​​题不在于此。

We'll probably need to see the rest of the code to understand why you're seeing what you're seeing; I cut and pasted your code verbatim, so odds are good the issue lies elsewhere.

dump(theArray);
Arrays.sort(theArray, new Comparator<Integer[]>() {
    public int compare(Integer[] int1, Integer[] int2) {
        Integer numOfKeys1 = int1[1];
        Integer numOfKeys2 = int2[1];
        return numOfKeys1.compareTo(numOfKeys2);
    }
});
System.out.println("================");
dump(theArray);


0 10 
0 10 
1 9 
2 9 
3 9 
4 15 
5 10 
6 4 
7 8 
8 11 
9 12 
================
6 4 
7 8 
1 9 
2 9 
3 9 
0 10 
0 10 
5 10 
8 11 
9 12 
4 15 

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

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