您的位置:首页 > 移动开发 > Objective-C

List<Object>根据Object里的2个属性排序

2016-09-23 11:33 621 查看
<pre style="font-family: 宋体; font-size: 13.5pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;

/**
* Created by huguoju on 2016/9/22.
* List按照指定字段排序工具类
* @param <T> 实体对象
*/
public class ListSortUtil<T> {
/**
* @param targetList 目标排序List
* @param field1 排序字段(实体类属性名)
* @param field2 排序字段(如果field1相同,按照field2排序)
* @param sortMode field1排序方式(asc or  desc)
* @param sortMode2 field2排序方式
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void sort(List<T> targetList, final String field1,final String field2, final String sortMode,String sortMode2) {
LogUtils.audit("排序前:"+targetList);
Collections.sort(targetList, new Comparator() {
public int compare(Object arg1, Object arg2) {
int retVal = 0;
try {
retVal=sortObj(field1,arg1,arg2);
if(retVal==0){
if (sortMode2 != null && "desc".equals(sortMode2)){
retVal=sortObj(field2,arg1,arg2);
}else {
retVal=sortObj(field2,arg2,arg1);
}

}
if (sortMode != null && "desc".equals(sortMode)) {
// 倒序
retVal = -retVal;
}
} catch (Exception e) {
throw new RuntimeException();
}
return retVal;
}
});
LogUtils.audit("排序后:"+targetList);
}
public int sortObj(String field,Object arg1, Object arg2) throws NoSuchMethodException,InvocationTargetException,IllegalAccessException{
//首字母转大写
String newStr1=field.substring(0, 1).toUpperCase()+field.replaceFirst("\\w","");
String newStr2=field.substring(0, 1).toUpperCase()+field.replaceFirst("\\w","");
String methodStr1="get"+newStr1;
String methodStr2="get"+newStr2;

Method method1 = ((T)arg1).getClass().getMethod(methodStr1, null);
Method method2 = ((T)arg2).getClass().getMethod(methodStr2, null);
Object obj1 = method1.invoke(((T)arg1), null);
Object obj2 = method2.invoke(((T)arg2), null);
int result=0;
if(obj1 instanceof String) {
// 字符串
result = obj1.toString().compareTo(obj2.toString());
}else if(obj1 instanceof Date) {
// 日期
long l = ((Date)obj1).getTime() - ((Date)obj2).getTime();
if(l > 0) {
result = 1;
}else if(l < 0) {
result = -1;
}else {
result = 0;
}
}else if(obj1 instanceof Integer) {
// 整型(Method的返回参数可以是int的,因为JDK1.5之后,Integer与int可以自动转换了)
result = (Integer)obj1 - (Integer)obj2;
}else if(obj1 instanceof BigDecimal){
result=((BigDecimal) obj1).subtract((BigDecimal) obj2).intValue();
}
return result;
}
}
使用:
<pre style="font-family: 宋体; font-size: 13.5pt; background-color: rgb(255, 255, 255);">ListSortUtil<User> sortUtil=<span style="color:#000080;"><strong>new </strong></span>ListSortUtil<User>();

sortUtil.sort(list,"name","createDate","desc","asc");//name和createDate为User对象的属性



String转List<Object>

JSONArray data = JSONArray.fromObject(liststring);
List<要转成的model> plist =

JSONArray.toList(data, 要转成的model.class);



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息