如何对二维数组进行排序

最近做题时发现有些任务需要将二维数组按照某一列进行排序

但JDK里并没有直接提供这一排序的方法

考虑自己重新写实在太麻烦(还不一定正确/高效)

百度了一番:

原地址:(https://zhidao.baidu.com/question/584126645687159005.html?qbl=relate_question_1&word=Arrays.sort%28ob%2C%20new%20Comparator%3CObject%3E%28%29%20%7B%7D%29)

import java.util.Arrays;
import java.util.Comparator;
 
/**
 * 二维数组排序示例
 * @author YY2924 2014/11/28
 * @version 1.0
 */
public class MatrixSort {
    public static void main(String[] args) {
         
        //二维数组
        Integer[][] matrix = new Integer[][] {
                {8,7},{9,5},{6,4}
        };
     
        //排序
        Arrays.sort(matrix,new Comparator() {
            @Override
            public int compare(Integer[] x, Integer[] y) {
                if(x[0] < y[0]){
                    return 1;
                } else if(x[0] > y[0]){
                    return -1;
                } else {
                    return 0;
                }
            }
        });
         
        //打印
        for(Integer[] integers : matrix){
            System.out.println(Arrays.toString(integers));
        }
    }
}

类似的还有:

 (原地址http://www.cnblogs.com/whaozl/p/4061993.html)

import java.util.Arrays;    
import java.util.Comparator;    
      
  public class ArraySort {    
      
      public static void sort(int[][] ob, final int[] order) {    
          Arrays.sort(ob, new Comparator() {    
              public int compare(Object o1, Object o2) {    
                  int[] one = (int[]) o1;    
                  int[] two = (int[]) o2;    
                  for (int i = 0; i < order.length; i++) {    
                      int k = order[i];    
                      if (one[k] > two[k]) {    
                          return 1;    
                      } else if (one[k] < two[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 } };    
          sort(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.csdn.net/zhengqiqiqinqin/article/details/9002209

关于Array,sort(arr,new Comparator(){} ) :

理解Comparator:http://blog.csdn.net/sinat_34979528/article/details/54407678

————浅显易懂的比较(假如A相同,则比较B):
class MyComparator implements Comparator{  
    @Override  
    public int compare(UserInfo o1,UserInfo o2) {  
          
        if(o1.getAge()-o2.getAge()==0){  
            return o1.getUserid()-o2.getUserid();  
        }else{  
            return o1.getAge()-o2.getAge();  
        }  
    }  
}  


理解Array.Sort():http://blog.csdn.net/wisgood/article/details/16541013
*Array.Sort()中具体实现,代码解读:http://blog.csdn.net/octopusflying/article/details/52388012
Collections中Sort()的简单解读:http://blog.csdn.net/bruce_6/article/details/38274919

你可能感兴趣的:(二维数组,排序,jdk,JAVA)