且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通用排序工具类

更新时间:2022-08-28 18:50:25

1,实际应用:

Java代码  通用排序工具类
  1. List<OrderInfoBean> orderInfoBeans = commitOrderDto.getValue();  
  2.         SortList<OrderInfoBean> sortList = new SortList<OrderInfoBean>();  
  3.         sortList.Sort(orderInfoBeans, "getCreateTime""desc");  

 

2,工具类sortList源码

Java代码  通用排序工具类
  1. package com.gov.util;  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4. import java.lang.reflect.Method;  
  5. import java.util.Collections;  
  6. import java.util.Comparator;  
  7. import java.util.List;  
  8.   
  9. /*** 
  10.  * 用户排序 
  11.  * @author huangwei 
  12.  * 2015年7月1日 
  13.  * @param <E> 
  14.  */  
  15. public class SortList<E> {  
  16.     public void Sort(List<E> list, final String method, final String sort) {  
  17.         Collections.sort(list, new Comparator() {  
  18.             public int compare(Object a, Object b) {  
  19.                 int ret = 0;  
  20.                 try {  
  21.                     Method m1 = ((E) a).getClass().getMethod(method, null);  
  22.                     Method m2 = ((E) b).getClass().getMethod(method, null);  
  23.                     if (sort != null && "desc".equals(sort))// 倒序  
  24.                         ret = m2.invoke(((E) b), null).toString()  
  25.                                 .compareTo(m1.invoke(((E) a), null).toString());  
  26.                     else  
  27.                         // 正序  
  28.                         ret = m1.invoke(((E) a), null).toString()  
  29.                                 .compareTo(m2.invoke(((E) b), null).toString());  
  30.                 } catch (NoSuchMethodException ne) {  
  31.                     System.out.println(ne);  
  32.                 } catch (IllegalAccessException ie) {  
  33.                     System.out.println(ie);  
  34.                 } catch (InvocationTargetException it) {  
  35.                     System.out.println(it);  
  36.                 }  
  37.                 return ret;  
  38.             }  
  39.         });  
  40.     }  
  41. }  

 

另外一种排序方式

实例:

Java代码  通用排序工具类
  1. List<CommonDictionary>list= DictionaryParam.getList("ptype");  
  2.               
  3.         Collections.sort(list,new SystemHWUtil. ListComparator(true,"value"));  
  4.         model.addAttribute("commonDictionaries", list);  

 说明:list的类型是ArrayList;

按照list中的元素(CommonDictionary对象)的成员变量value进行排序

SystemHWUtil. ListComparator的源代码:

Java代码  通用排序工具类
  1. public static class ListComparator implements Comparator{  
  2.         /*** 
  3.          * 是否转化为Int之后再比较 
  4.          */  
  5.         private boolean isConvertInteger;  
  6.         /*** 
  7.          * 对哪个列进行排序 
  8.          */  
  9.         private String comparedProperty;  
  10.         public ListComparator(boolean isConvertInteger,String comparedProperty) {  
  11.             super();  
  12.             this.isConvertInteger = isConvertInteger;  
  13.             this.comparedProperty=comparedProperty;  
  14.         }  
  15.         public int compare(Object o1, Object o2) {  
  16.             if(null!=o1&&null!=o2)  
  17.             {  
  18.                 try {  
  19.                     Object obj1=ReflectHWUtils.getObjectValue(o1, comparedProperty);  
  20.                     Object obj2=ReflectHWUtils.getObjectValue(o2, comparedProperty);  
  21.                     if(isConvertInteger){  
  22.                         int num1;  
  23.                         int num2;  
  24.                         if(obj1 instanceof Integer){  
  25.                             num1=(Integer)obj1;  
  26.                             num2=(Integer)obj2;  
  27.                         }else{  
  28.                             num1=Integer.parseInt(obj1.toString());  
  29.                             num2=Integer.parseInt(obj2.toString());  
  30.                         }  
  31.                         if(num1>num2){  
  32.                             return 1;  
  33.                         }else if(num1<num2){  
  34.                             return -1;  
  35.                         }else{  
  36.                             return 0;  
  37.                         }  
  38.                     }else{  
  39.                         return obj1.toString().compareTo(obj2.toString());  
  40.                     }  
  41.                 } catch (SecurityException e) {  
  42.                     e.printStackTrace();  
  43.                 } catch (NoSuchFieldException e) {  
  44.                     e.printStackTrace();  
  45.                 } catch (IllegalArgumentException e) {  
  46.                     e.printStackTrace();  
  47.                 } catch (IllegalAccessException e) {  
  48.                     e.printStackTrace();  
  49.                 }  
  50.             }  
  51.             return 0/*等于*/;  
  52.         }  
  53.     }  

 

附录:

Java代码  通用排序工具类
  1. public static class ArrayListComparator implements Comparator{  
  2.         /*** 
  3.          * 排序的依据 
  4.          */  
  5.         private String titles[];  
  6.         /*** 
  7.          * 对哪个列进行排序 
  8.          */  
  9.         private String comparedProperty;  
  10.           
  11.         public ArrayListComparator(String[] titles,String comparedProperty) {  
  12.             super();  
  13.             this.titles = titles;  
  14.             this.comparedProperty=comparedProperty;  
  15.         }  
  16.   
  17.         public int compare(Object o1, Object o2) {  
  18.             if(null!=o1&&null!=o2)  
  19.             {  
  20.                   
  21.                 try {  
  22.                     if(SystemHWUtil.indexOfArr(titles,(String)ReflectHWUtils.getObjectValue(o1, comparedProperty)   ) >  
  23.                     SystemHWUtil.indexOfArr(titles,(String)ReflectHWUtils.getObjectValue(o2, comparedProperty))){  
  24.                         return 1/*大于*/;  
  25.                     }else {  
  26.                         return -1/*小于*/;  
  27.                     }  
  28.                 } catch (SecurityException e) {  
  29.                     e.printStackTrace();  
  30.                 } catch (NoSuchFieldException e) {  
  31.                     e.printStackTrace();  
  32.                 } catch (IllegalArgumentException e) {  
  33.                     e.printStackTrace();  
  34.                 } catch (IllegalAccessException e) {  
  35.                     e.printStackTrace();  
  36.                 }  
  37.             }  
  38.             return 0/*等于*/;  
  39.         }  
  40.           
  41.     }