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

  1. import java.util.*;  
  2.   
  3.     public static void sortIntArray(int[][] arObjects, final int[] arOrders)  
  4.     {  
  5.         Arrays.sort(arObjects, new Comparator<Object>()  
  6.         {  
  7.             public int compare(Object oObjectA, Object oObjectB)  
  8.             {  
  9.                 int[] arTempOne = (int[])oObjectA;  
  10.                 int[] arTempTwo = (int[])oObjectB;  
  11.                 for (int i = 0; i < arOrders.length; i++)  
  12.                 {  
  13.                     int k = arOrders[i];  
  14.                     if (arTempOne[k] > arTempTwo[k])  
  15.                     {  
  16.                         return 1;  
  17.                     }  
  18.                     else if (arTempOne[k] < arTempTwo[k])  
  19.                     {  
  20.                         return -1;  
  21.                     }  
  22.                     else  
  23.                     {  
  24.                         continue;  
  25.                     }  
  26.                 }  
  27.                 return 0;  
  28.             }  
  29.         });  
  30.     }  

调用方法是:
  1. public static void main(String[] args)  
  2. {  
  3.     int array[][] = new int[][] {       
  4.             { 12346832912545 },       
  5.             { 34728257560213 },       
  6.             { 123468322194523 },       
  7.             { 911032354733418 },      
  8.             { 128318926279833 },       
  9.             { 47238892489923657 },       
  10.             { 123468343878235768 },       
  11.             { 123498567812546 },       
  12.             { 267823657834256873 } };      
  13.     sortIntArray(array, new int[] { 01 });    // 先根据第一列比较,若相同则再比较第二列  
  14.     for (int i = 0; i < array.length; i++)  
  15.     {  
  16.         for (int j = 0; j < array[i].length; j++)  
  17.         {  
  18.             System.out.print(array[i][j]);  
  19.             System.out.print("\t");  
  20.         }  
  21.         System.out.println();  
  22.     }  
  23. }  






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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值