Java 实现二维数组按指定列经行排序

import java.util.*;

    public static void sortIntArray(int[][] arObjects, final int[] arOrders)
    {
        Arrays.sort(arObjects, new Comparator<Object>()
        {
            public int compare(Object oObjectA, Object oObjectB)
            {
                int[] arTempOne = (int[])oObjectA;
                int[] arTempTwo = (int[])oObjectB;
                for (int i = 0; i < arOrders.length; i++)
                {
                    int k = arOrders[i];
                    if (arTempOne[k] > arTempTwo[k])
                    {
                        return 1;
                    }
                    else if (arTempOne[k] < arTempTwo[k])
                    {
                        return -1;
                    }
                    else
                    {
                        continue;
                    }
                }
                return 0;
            }
        });
    }

调用方法是:

    public static void main(String[] args)
    {
        int array[][] = new int[][] {     
                { 12, 34, 68, 32, 9, 12, 545 },     
                { 34, 72, 82, 57, 56, 0, 213 },     
                { 12, 34, 68, 32, 21, 945, 23 },     
                { 91, 10, 3, 2354, 73, 34, 18 },    
                { 12, 83, 189, 26, 27, 98, 33 },     
                { 47, 23, 889, 24, 899, 23, 657 },     
                { 12, 34, 68, 343, 878, 235, 768 },     
                { 12, 34, 98, 56, 78, 12, 546 },     
                { 26, 78, 2365, 78, 34, 256, 873 } };    
        sortIntArray(int[][], int[])(array, new int[] { 0, 1 });    // 先根据第一列比较,若相同则再比较第二列
        for (int i = 0; i < array.length; i++)
        {
            for (int j = 0; j < array[i].length; j++)
            {
                System.out.print(array[i][j]);
                System.out.print("\t");
            }
            System.out.println();
        }
    }






注:本文转载自:http://blog.sina.com.cn/s/blog_5a15b7d101014ngm.html



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值