java List Collections.sort 排序 支持 对象属性 普通数据类型
2011-09-26 09:52
1046 查看
升序 降序 正序 倒序 排列
Java代码import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
public class ListComparator implements Comparator {
public ListComparator() {}
public ListComparator(String methodName) {
this.methodName = methodName;
}
//空:普通的数据类型 非空:对象数据类型 取methodName返回值比较
String methodName;
public Object getValue(Object bean, String methodName) throws Exception {
Method getMethod = bean.getClass().getMethod(methodName, null);
return getMethod.invoke(bean, null);
}
public int compare(Object obj1, Object obj2) {
int val = -1;
try {
if ( methodName == null ) {
val = _compare(obj1, obj2);
} else {
val = _compare(getValue(obj1, methodName), getValue(obj2, methodName));
}
} catch (Exception ex) {ex.printStackTrace();}
return val;
}
public int _compare(Object obj1, Object obj2) throws Exception {
if ( obj1 == null || obj2 == null ) return obj1 == null ? -1 : 1;
Class cl = obj1.getClass();
if ( obj1 instanceof java.lang.Comparable ) {
//byte int long float..number, date , boolean , char
Method getMethod = obj1.getClass().getMethod("compareTo", new Class[]{cl});
return (Integer) getMethod.invoke(obj1, new Object[]{obj2});
}
return -1;
}
}
//测试类
class Test {
Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public static void main(String[] args) {
//普通数据类型 升序
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
List list = new ArrayList();
for (long i = 0; i < 5; i++) {
list.add( new Date(System.currentTimeMillis() + (int) (Math.random() * 1000000000)) );
}
ListComparator c = new ListComparator();
Collections.sort(list, c);
for (int i = 0; i < list.size(); i++) {
System.out.println(simpleDateFormat.format(list.get(i)));
}
//对象数据类型 降序
List<Test> userList = new ArrayList();
Test test = new Test();
test.setId(null);
userList.add(test);
for (long i = 0; i < 5; i++) {
test = new Test();
test.setId(i);
userList.add(test);
}
c = new ListComparator("getId");
Collections.sort(userList,Collections.reverseOrder(c));
for (int i = 0; i < userList.size(); i++) {
System.out.println(userList.get(i).getId());
}
}
}
相关文章推荐
- java List Collections.sort 排序 支持 对象属性 普通数据类型 升序 降序 正序 倒序 排列
- List自定义排序 让Collections.sort 排序支持对象属性
- Java 中List数据,自定义对象按指定的属性进行排序
- 用Collections.sort方法对list排序有两种方法 第一种是list中的对象实现Comparable接口,如下: [java] view plain copy /** * 根据ord
- java根据时间排序list,使用collections.sort()
- 【Java】如何给List集合排序Collections.sort(list,new Comparator<Entity>)
- java List 排序 Collections.sort() 对 List 排序
- 用Java集合中的Collections.sort方法对list排序的两种方法
- LinkedList中将对象按照某一属性排序,利用 collection.sort(); 来进行调用!
- java list中的对象,按对象某属性排序
- java List 排序 Collections.sort() 对 List 排序
- Java List重载Collections.sort方法排序
- java根据时间排序list,使用collections.sort()
- java List 排序 Collections.sort
- java list按照元素对象的指定多个字段属性进行排序
- java List 排序 Collections.sort() 对 List 排序
- [转]java List 排序 Collections.sort() 对 List 排序
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
- java List 排序 Collections.sort() 对 List 排序
- java例程练习(对象类型数据的排序)