//网上找的一个例子自己修改了下分享下,可以传多个排序字段数组
public class SortList<E>{ public static Logger loger = LoggerFactory.getLogger(SortList.class);
/**
*
* 方法描述:自定义排序(这里暂时只支持3种属性同时排序,后面要加属性可扩展下面哪种类型的排序的if else)
* 这里是根据中英文首字母第一排序
* @param 1 list 要排序的对象集合
* @param 2 methods 要排序的对象属性
* @param 3 sorts 属性是降序还是升序 如果传NULL 默认升序(sorts数组跟methods是一个一个对应的)
* @Title: Sort *
* @modifier
*/
@SuppressWarnings("all")
public void Sort(List<E> list, final String[] methods, final String[] sorts){
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0;
try{
if(methods==null || methods.length==0){
loger.error("未传入排序字段");
}else{
Method m1 = ((E)a).getClass().getMethod(methods[0], null);
Method m2 = ((E)b).getClass().getMethod(methods[0], null);
if(sorts == null || sorts.length==0 || "asc".equals(sorts[0])){
ret = ChinaInitial.getPYIndexStr(m1.invoke(((E)a), null).toString(),false).substring(0, 1).compareTo(ChinaInitial.getPYIndexStr(m2.invoke(((E)b), null).toString(),false).substring(0, 1));
}else{
ret = ChinaInitial.getPYIndexStr(m2.invoke(((E)b), null).toString(),false).substring(0, 1).compareTo(ChinaInitial.getPYIndexStr(m1.invoke(((E)a), null).toString(),false).substring(0, 1));
}
if(methods.length>1){
for(int i = 0;i<methods.length;i++){
if(i==0) continue;
String method = methods[i];
Method m3 = ((E)a).getClass().getMethod(method, null);
Method m4 = ((E)b).getClass().getMethod(method, null);
Object m3Obj = m3.invoke(((E)a), null);
Object m4Obj = m4.invoke(((E)b), null);
if(m3Obj instanceof Date){
if(ret==0){
if(sorts==null || sorts.length==0 || sorts[i].equals("asc")){
ret = ((Date)m4Obj).compareTo((Date)m3Obj);
}else{
ret = ((Date)m3Obj).compareTo((Date)m4Obj);
}
}
}else if(m3Obj instanceof Integer){
if(ret==0){
if(sorts==null || sorts.length==0 || sorts[i].equals("asc")){
ret = ((Integer)m4Obj).compareTo((Integer)m3Obj);
}else{
ret = ((Integer)m3Obj).compareTo((Integer)m4Obj);
}
}
}
}
}
}
}catch(Exception e){
loger.error("异常",e);
}
return ret;
}
});
}
}
//下面是获取中文首字母的类
/***
*
* 得到中文首字母
*
* @author lxm_09
*/ public class ChinaInitial { /**
*
* @param args
*/ public static void main(String[] args) { String str = "我是中国人"; System.out.println("中文首字母:" + getPYIndexStr(str, false)); } /**
*
* 返回首字母
*
* @param strChinese
*
* @param bUpCase
*
* @return
*/ public static String getPYIndexStr(String strChinese, boolean bUpCase) { try { StringBuffer buffer = new StringBuffer(); byte b[] = strChinese.getBytes("GBK");// 把中文转化成byte数组 for (int i = 0; i < b.length; i++) { if ((b[i] & 255) > 128) { int char1 = b[i++] & 255; char1 <<= 8;// 左移运算符用“<<”表示,是将运算符左边的对象,向左移动运算符右边指定的位数,并且在低位补零。其实,向左移n位,就相当于乘上2的n次方 int chart = char1 + (b[i] & 255); buffer.append(getPYIndexChar((char) chart, bUpCase)); continue; } char c = (char) b[i]; if (!Character.isJavaIdentifierPart(c))// 确定指定字符是否可以是 Java
// 标识符中首字符以外的部分。 c = 'A'; buffer.append(c); } return buffer.toString(); } catch (Exception e) { System.out.println((new StringBuilder()).append("\u53D6\u4E2D\u6587\u62FC\u97F3\u6709\u9519")
.append(e.getMessage()).toString()); } return null; } /**
*
* 得到首字母
*
* @param strChinese
*
* @param bUpCase
*
* @return
*/ private static char getPYIndexChar(char strChinese, boolean bUpCase) { int charGBK = strChinese; char result; if (charGBK >= 45217 && charGBK <= 45252) result = 'A'; else if (charGBK >= 45253 && charGBK <= 45760) result = 'B'; else if (charGBK >= 45761 && charGBK <= 46317) result = 'C'; else if (charGBK >= 46318 && charGBK <= 46825) result = 'D'; else if (charGBK >= 46826 && charGBK <= 47009) result = 'E'; else if (charGBK >= 47010 && charGBK <= 47296) result = 'F'; else if (charGBK >= 47297 && charGBK <= 47613) result = 'G'; else if (charGBK >= 47614 && charGBK <= 48118) result = 'H'; else if (charGBK >= 48119 && charGBK <= 49061) result = 'J'; else if (charGBK >= 49062 && charGBK <= 49323) result = 'K'; else if (charGBK >= 49324 && charGBK <= 49895) result = 'L'; else if (charGBK >= 49896 && charGBK <= 50370) result = 'M'; else if (charGBK >= 50371 && charGBK <= 50613) result = 'N'; else if (charGBK >= 50614 && charGBK <= 50621) result = 'O'; else if (charGBK >= 50622 && charGBK <= 50905) result = 'P'; else if (charGBK >= 50906 && charGBK <= 51386) result = 'Q'; else if (charGBK >= 51387 && charGBK <= 51445) result = 'R'; else if (charGBK >= 51446 && charGBK <= 52217) result = 'S'; else if (charGBK >= 52218 && charGBK <= 52697) result = 'T'; else if (charGBK >= 52698 && charGBK <= 52979) result = 'W'; else if (charGBK >= 52980 && charGBK <= 53688) result = 'X'; else if (charGBK >= 53689 && charGBK <= 54480) result = 'Y'; else if (charGBK >= 54481 && charGBK <= 55289) result = 'Z'; else result = (char) (65 + (new Random()).nextInt(25)); if (!bUpCase) result = Character.toLowerCase(result); return result; } }
//调用示例
SortList<Object> sortList = new SortList<Object>();
String[] param = {sortPropertyName};
String[] sorts = {sortRule};
sortList.Sort(resultList, param, sorts);