步骤,根据列进行排序二维int数组 [英] procedure to sort a two dimensional int array depending on column

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

问题描述

我会告诉你一个例​​子关于这个问题的目的.The阵列之前,我已经和我们如何希望排序后:

i will show you an example about the purpose of the question .The array i have before and how we want it after the sorting :

Box    Weight    Priority
1       50          5
2       30          8
3       90          6
4       20          7  
5       80          9

Box    Weight    Priority
3       90          6
5       80          9
1       50          5
2       30          8
4       20          7

我们在INT矩阵工作:

we work in the int matrix :

data= new int[BoxNumber][3];

排序是基于在寻找的数据数组排序过程的第二列Weight.Am。

The sorting is based in the second column Weight.Am looking for a procedure that sort the data array.

 public void sortC(int[][] temp)
{
    if (temp.length >= 2)
    {
        for (int i = 1; i <= temp.length - 1; i++)
        {
            int[] hold = temp[i];
            int[] holdP = temp[i-1];

            int j = i;

            while (j > 0 && hold[1] < holdP[1]) // 1 represents the reference of sorting
            {
                hold = temp[j];
                holdP = temp[j-1];

                temp[j] = holdP;
                temp[j-1] = hold;

                j--;
            }
        }
    }
}

 sortC(data);

我想这一个,但遗憾的是并没有给出一个正确的排序我无法弄清楚的咸菜。
一些帮助PLZ?

I tried this one, but unfortunately is doesn't give a right sorting i couldn't figure out the pickle. Some help plz ?

推荐答案

使用 java.util.Arrays.sort 使用自定义的比较

int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },
        { 4, 20, 7 }, { 5, 80, 9 }, };
Arrays.sort(temp, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o2[1], o1[1]);
    }
});

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

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