设为首页 加入收藏

TOP

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

int ret = 0;
try {
Method m = a.getClass().getMethod(method, null);
m.setAccessible(true);
Class< > type = m.getReturnType();
if (type == int.class) {
ret = ((Integer) m.invoke(a, null))
.compareTo((Integer) m.invoke(b, null));
} else if (type == double.class) {
ret = ((Double) m.invoke(a, null))
.compareTo((Double) m.invoke(b, null));
} else if (type == long.class) {
ret = ((Long) m.invoke(a, null))
.compareTo((Long) m.invoke(b, null));
} else if (type == float.class) {
ret = ((Float) m.invoke(a, null))
.compareTo((Float) m.invoke(b, null));
} else if (type == Date.class) {
ret = ((Date) m.invoke(a, null))
.compareTo((Date) m.invoke(b, null));
} else if (isImplementsOf(type, Comparable.class)) {
ret = ((Comparable) m.invoke(a, null))
.compareTo((Comparable) m.invoke(b,
null));
} else {
ret = String.valueOf(m.invoke(a, null))
.compareTo(
String.valueOf(m
.invoke(b, null)));
}


} catch (NoSuchMethodException ne) {
System.out.println(ne);
} catch (IllegalAccessException ie) {
System.out.println(ie);
} catch (InvocationTargetException it) {
System.out.println(it);
}


if (sort != null && sort.toLowerCase().equals(DESC)) {
return -ret;
} else {
return ret;
}
}
});
}
}
return list;
}


/**
* 判断对象实现的所有接口中是否包含szInterface
*
* @param clazz
* @param szInterface
* @return
*/
public static boolean isImplementsOf(Class< > clazz, Class< > szInterface) {
boolean flag = false;


Class< >[] face = clazz.getInterfaces();
for (Class< > c : face) {
if (c == szInterface) {
flag = true;
} else {
flag = isImplementsOf(c, szInterface);
}
}


if (!flag && null != clazz.getSuperclass()) {
return isImplementsOf(clazz.getSuperclass(), szInterface);
}


return flag;
}


public static void main(String[] args) throws Exception {
List list = new ArrayList();


list.add(new Student(3, "b", 1, new Date(11110000)));
list.add(new Student(1, "c", 3, new Date(44440000)));
list.add(new Student(2, "a", 2, new Date(22210000)));
list.add(new Student(4, "a", 11, new Date(33330000)));
System.out.println("-------原来序列-------------------");
for (Student stu : list) {
System.out.println(stu.toString());
}


// 按age正序排序,注意结果排完后是1,2,3,11. 不是1,11,2,3(如果是String类型正序排序是这样)
SortListUtil.sort(list, "age", null);
System.out.println("---------测试Integer和正序,按age正序排序-----------------");
for (Student stu : list) {
System.out.println(stu.toString());
}


// 按id倒序
SortListUtil.sort(list, "id", SortListUtil.DESC);
System.out.println("--------测试int和倒序,按id倒序------------------");
for (Student stu : list) {
System.out.println(stu.toString());
}


// 先按name正序排序,再按id正序排序
SortListUtil.sort(list, new String[] { "name", "id" }, new String[] {});
System.out
.println("---------测试多个排序字段,先按name正序,name相同时再按id正序-----------------");
for (Student stu : list) {
System.out.println(stu.toString());
}


// 先按name正序排序,再按id倒序排序
SortListUtil.sort(list, new String[] { "name", "id" }, new String[] {
SortListUtil.ASC, SortListUtil.DESC });
System.out
.println("---------测试多个排序字段,先按name正序,name相同时再按id倒序-----------------");
for (Student stu : list) {
System.out.println(stu.toString());
}


// 按birthday排序
SortListUtil.sort(list, "birthday");
System.out
.println("----

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

评论

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