集合排序

集合排序(BigDecimal、根据对象某个属性排序)
 
 
      /**
      * 按照属性property给list排序
      * 支持的属性类型:Short Integer Long Float Double BigDecimal Date
      *  @param  <E>
      *  @param  list
      *  @param  property
      *  @param  clazz
      *  @param  ascOrDesc  (true 是 asc, false 是 desc)
      */
      public  static <E>  void  sortList(List<E> list,  final  String property,  final  boolean  ascOrDesc) {
           
             if  (list ==  null  || list.isEmpty()) {
                return ;
           }
           
           Collections. sort(list,  new  Comparator<E>() {
 
                  @Override
                  public  int  compare(E o1, E o2) {
                       try  {
                           Field field1 = o1.getClass().getField(property);
                           Field field2 = o2.getClass().getField(property);
                             try  {
                                Class<?> clazz =  field1.get(o1).getClass();
                                  if  (clazz == Short.  class  || clazz == Integer. class  || clazz == Long. class
                                           || clazz == Float. class  || clazz == Double. class ) {
                                       if  (ascOrDesc) {       //asc
                                             return  field1.getInt(o1) - field2.getInt(o2);
                                     }  else  {               //desc
                                             return  field2.getInt(o2) - field1.getInt(o1);               
                                     }    
                                }
                                  if  (clazz == BigDecimal.  class ) {
                                     BigDecimal b1 = (BigDecimal)field1.get(o1);
                                     BigDecimal b2 = (BigDecimal)field2.get(o2);
                                       if  (ascOrDesc) {       //asc
                                             return  b1.compareTo(b2);
                                     }  else  {               //desc
                                             return  b2.compareTo(b1);               
                                     }    
                                }
                                  if  (clazz == Date.  class ) {
                                     Date b1 = (Date)field1.get(o1);
                                     Date b2 = (Date)field2.get(o2);
                                       if  (ascOrDesc) {       //asc
                                             return  b1.compareTo(b2);
                                     }  else  {               //desc
                                             return  b2.compareTo(b1);               
                                     }    
                                }
                           }  catch  (IllegalArgumentException e) {
                                e.printStackTrace();
                           }  catch  (IllegalAccessException e) {
                                e.printStackTrace();
                           }
                     }  catch  (SecurityException e) {
                           e.printStackTrace();
                     }  catch  (NoSuchFieldException e) {
                           e.printStackTrace();
                     }
                     
                       return  0;
                }
                
           });
     }
     
      /**
      * 按照属性property给array排序
      * 支持的属性类型:Short Integer Long Float Double BigDecimal Date
      *  @param  <E>
      *  @param  e
      *  @param  property
      *  @param  ascOrDesc (true 是 asc, false 是 desc)
      */
      public  static <E>  void  sortArray(E[] e,  final  String property,  final  boolean  ascOrDesc) {
           
             if  (e ==  null  || e.  length  == 0) {
                return ;
           }
           
           Arrays. sort(e,  new  Comparator<E>() {
 
                  @Override
                  public  int  compare(E o1, E o2) {
                       try  {
                           Field field1 = o1.getClass().getField(property);
                           Field field2 = o2.getClass().getField(property);
                             try  {
                                Class<?> clazz =  field1.get(o1).getClass();
                                  if  (clazz == Short.  class  || clazz == Integer. class  || clazz == Long. class
                                           || clazz == Float. class  || clazz == Double. class ) {
                                       if  (ascOrDesc) {       //asc
                                             return  field1.getInt(o1) - field2.getInt(o2);
                                     }  else  {               //desc
                                             return  field2.getInt(o2) - field1.getInt(o1);               
                                     }    
                                }
                                  if  (clazz == BigDecimal.  class ) {
                                     BigDecimal b1 = (BigDecimal)field1.get(o1);
                                     BigDecimal b2 = (BigDecimal)field2.get(o2);
                                       if  (ascOrDesc) {       //asc
                                             return  b1.compareTo(b2);
                                     }  else  {               //desc
                                             return  b2.compareTo(b1);               
                                     }    
                                }
                                  if  (clazz == Date.  class ) {
                                     Date b1 = (Date)field1.get(o1);
                                     Date b2 = (Date)field2.get(o2);
                                       if  (ascOrDesc) {       //asc
                                             return  b1.compareTo(b2);
                                     }  else  {               //desc
                                             return  b2.compareTo(b1);               
                                     }    
                                }
                           }  catch  (IllegalArgumentException e) {
                                e.printStackTrace();
                           }  catch  (IllegalAccessException e) {
                                e.printStackTrace();
                           }
                     }  catch  (SecurityException e) {
                           e.printStackTrace();
                     }  catch  (NoSuchFieldException e) {
                           e.printStackTrace();
                     }
                     
                       return  0;
                }
                
           });
     }
     
      /**
      * 直接用Collections.sort(list  <BigDecimal> );即可,如果是BigDecimal数组则需要像下面这样重写
      *  @param  bds
      *  @param  ascOrDesc
      */
      public  static  void  sortBigDecimalArray(BigDecimal[] bds,  final  boolean  ascOrDesc) {
           
             if  (bds ==  null  || bds.  length  == 0) {
                return ;
           }
           
           Arrays. sort(bds,  new  Comparator<BigDecimal>() {
 
                  @Override
                  public  int  compare(BigDecimal o1, BigDecimal o2) {
                       if  (ascOrDesc) {       //asc
                             return  o1.compareTo(o2);
                     }  else  {               //desc
                             return  o2.compareTo(o1);                        
                     }
                }
           });
           
     }

你可能感兴趣的:(排序)