设为首页 加入收藏

TOP

Java通用工具类之按对象属性排序工具(一)
2014-11-24 02:31:56 来源: 作者: 【 】 浏览:0
Tags:Java 通用 工具 对象 属性 排序

本工具类为按对象属性排序工具类,实现的功能:
1.按对象的一个属性和多个属性进行排序.
2.按对象属性正序和倒序排列.
3.完美支持int等基础类和Integer等包装类.
4.完美支持属性为实现了Comparable接口的类.
5.如果类不是java.lang中定义的基础类型也没有实现Comparable接口则转为String后进行排序.


实现思路:使用反射取得对象属性或对象方法的值从而解除对具体对象的依赖.


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;


/**
* 通用工具类之按对象中某属性排序
* @author 李坤
* 交流博客:http://blog.csdn.net/lk_blog
*/
public class SortListUtil {
public static final String DESC = "desc";
public static final String ASC = "asc";


/**
* 对list中的元素按升序排列.
*
* @param list
* 排序集合
* @param field
* 排序字段
* @return
*/
public static List< > sort(List< > list, final String field) {
return sort(list, field, null);
}


/**
* 对list中的元素进行排序.
*
* @param list
* 排序集合
* @param field
* 排序字段
* @param sort
* 排序方式: SortList.DESC(降序) SortList.ASC(升序).
* @return
*/
@SuppressWarnings("unchecked")
public static List< > sort(List< > list, final String field,
final String sort) {
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0;
try {
Field f = a.getClass().getDeclaredField(field);
f.setAccessible(true);
Class< > type = f.getType();


if (type == int.class) {
ret = ((Integer) f.getInt(a)).compareTo((Integer) f
.getInt(b));
} else if (type == double.class) {
ret = ((Double) f.getDouble(a)).compareTo((Double) f
.getDouble(b));
} else if (type == long.class) {
ret = ((Long) f.getLong(a)).compareTo((Long) f
.getLong(b));
} else if (type == float.class) {
ret = ((Float) f.getFloat(a)).compareTo((Float) f
.getFloat(b));
} else if (type == Date.class) {
ret = ((Date) f.get(a)).compareTo((Date) f.get(b));
} else if (isImplementsOf(type, Comparable.class)) {
ret = ((Comparable) f.get(a)).compareTo((Comparable) f
.get(b));
} else {
ret = String.valueOf(f.get(a)).compareTo(
String.valueOf(f.get(b)));
}


} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (sort != null && sort.toLowerCase().equals(DESC)) {
return -ret;
} else {
return ret;
}


}
});
return list;
}


/**
* 对list中的元素按fields和sorts进行排序,
* fields[i]指定排序字段,sorts[i]指定排序方式.如果sorts[i]为空则默认按升序排列.
*
* @param list
* @param fields
* @param sorts
* @return
*/
@SuppressWarnings("unchecked")
public static List< > sort(List< > list, String[] fields, String[] sorts) {
if (fields != null && fields.length > 0) {
for (int i = fields.length - 1; i >= 0; i--) {
final String field = fields[i];
String tmpSort = ASC;
if (sorts != null && sorts.length > i && sorts[i] != null) {
tmpSort = sorts[i];
}
final String sort = tmpSort;
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0;
try {
Field f = a.getClass().getDeclaredField(field);
f.setAccessible(true);
Class< > type = f.getType();
if (type == int.class) {
ret = ((Integer) f.getInt(a))
.compareTo((Integer) f.getInt(b));
} else if (type == double.class) {
ret = ((Double) f.getDouble(a))
.compare

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Shell编程之条件测试 下一篇Spring-3.2.4 + Quartz-2.2.0集成..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: