zoukankan      html  css  js  c++  java
  • Java 多维数组 按某列 排序

        public MetaCell[][] getByColumn(final int columnIndex, int decisionIndex) {//【注意】final咯
            MetaCell[][] array = new MetaCell[m][2];//Entry<MetaCell, MetaCell>参考http://blog.csdn.net/sunmenggmail/article/details/8952712 和 http://www.cnblogs.com/fstang/archive/2013/04/20/3032097.html
            for(int i=0; i<m; i++){
                array[i][0]=A[i][columnIndex];
                array[i][1]=A[i][decisionIndex];
            }
            Arrays.sort(array, new Comparator<MetaCell[]>(){//二维数组按照某列进行排序,你也可以采用Map
                public int compare(MetaCell[] o1, MetaCell[] o2) {//任何多维数组可看成一个一维数组,一维数组中每个元素是一个一维数组
                    return o1[columnIndex].compareTo(o2[columnIndex]);//比较:大于0则表示升序
                }
            } );
            return array;
        }

     以上默认升序。可修改Comparator接口即可。

    以下是按多列排序【以第1列为准,第2列次之——当第一列出现相同值,用第2列排序】 

     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<Object>() {    
                  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(" ");    
                  }    
                  System.out.println();    
              }    
          }    
      }  
  • 相关阅读:
    牛客网在线编程:解救小易
    牛客网在线编程:身份证分组
    牛客网在线编程:优雅的点
    用FlexSlider制作支付宝2013版幻灯片演示插件
    Mysql Join语法解析与性能分析详解
    SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段
    jQuery 分页插件 jqPagination的使用
    Android图片异步加载之Android-Universal-Image-Loader
    C#随机函数random()典型用法集锦
    CSS自适应布局(左右固定 中间自适应或者右侧固定 左侧自适应)
  • 原文地址:https://www.cnblogs.com/whaozl/p/4061993.html
Copyright © 2011-2022 走看看