2019独角兽企业重金招聘Python工程师标准>>>
package com.hna.hka.mfm.gbs.car.uti;import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;import com.sun.org.apache.xpath.internal.operations.Number;/*** @author jardot* @version 1.0 通用排序*/
public class SortList {@SuppressWarnings({ "unchecked", "rawtypes" })public void Sort(List list, final String method, final String sort) {Collections.sort(list, new Comparator() {@Overridepublic int compare(Object a, Object b) {int ret = 0;try {Method m1 = ((E) a).getClass().getMethod(method, null);Method m2 = ((E) b).getClass().getMethod(method, null);String aValue = m1.invoke(((E) a), null).toString().trim()!=null?m1.invoke(((E) a), null).toString().trim():"0";String bValue = m1.invoke(((E) b), null).toString().trim()!=null?m1.invoke(((E) b), null).toString().trim():"0";int aIntValue = (int)(Double.parseDouble(aValue)*100);int bIntValue = (int)(Double.parseDouble(bValue)*100);if (sort != null && "desc".equals(sort)){// 倒序/*ret = m2.invoke(((E) b), null).toString().compareTo(m1.invoke(((E) a), null).toString());*///ret = aInt - bInt;ret = bIntValue - aIntValue;}else{// 正序/*ret = m1.invoke(((E) a), null).toString().compareTo(m2.invoke(((E) b), null).toString());*/ret = aIntValue - bIntValue;;}} catch (NoSuchMethodException ne) {System.out.println(ne);} catch (IllegalAccessException ie) {System.out.println(ie);} catch (InvocationTargetException it) {System.out.println(it);}return ret;}});}
}