首页 > 代码库 > 按对象的属性进行排序
按对象的属性进行排序
package com;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
/**
* 对list集合进行排序
* @author gouwei
*
*/
public class SortList<E> {
@SuppressWarnings("unchecked")
public void sort(List<E> list, final String method, final String sort){
if(list != null){
Collections.sort(list, new Comparator() {
public 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);
if(sort != null && "desc".equals(sort))//倒序
ret = m2.invoke(((E)b), null).toString().compareTo(m1.invoke(((E)a), null).toString());
else//正序
ret = m1.invoke(((E)a), null).toString().compareTo(m2.invoke(((E)b), null).toString());
}catch(NoSuchMethodException ne){
System.out.println(ne);
}catch(IllegalAccessException ie){
System.out.println(ie);
}catch(InvocationTargetException it){
System.out.println(it);
}
return ret;
}
});
}
}
/**
* @param args
*/
public static void main(String[] args) {
List<UserInfo> list = new ArrayList<UserInfo>();
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
try {
list.add(new UserInfo(3,"b",formater.parse("1980-12-01"),1d,"09:20"));
list.add(new UserInfo(100011,"c",formater.parse("1980-10-01"),989d,"19:10"));
list.add(new UserInfo(2,"a",formater.parse("1973-10-01"),10d,"15:30"));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("-------原来序列-------------------");
for(UserInfo user : list){
System.out.println(user.toString()+":"+user.getA());
}
// //调用排序通用类
// SortList<UserInfo> sortList = new SortList<UserInfo>();
//
// //按A排序
// sortList.sort(list, "getA", "desc");
// System.out.println("--------按a倒序------------------");
// for(UserInfo user : list){
// System.out.println(user.toString()+":"+user.getA());
// }
// //按A排序
// sortList.sort(list, "getA", null);
// System.out.println("--------按a正序------------------");
// for(UserInfo user : list){
// System.out.println(user.toString()+":"+user.getA());
// }
//
// //按B排序
// sortList.sort(list, "getB", "desc");
// System.out.println("---------按b排序-----------------");
// for(UserInfo user : list){
// System.out.println(user.toString()+":"+user.getB());
// }
//
// //按time排序
// sortList.sort(list, "getTime", null);
// System.out.println("---------按time排序-----------------");
// for(UserInfo user : list){
// System.out.println(user.toString()+":"+user.getTime());
// }
// //按C排序
// sortList.sort(list, "getC", null);
// System.out.println("---------按C排序-----------------");
// for(UserInfo user : list){
// System.out.println(user.toString()+":"+user.getC());
// }
// System.out.println("---------按C倒序-----------------");
// sortC(list,"desc");
// //按C排序
// for(UserInfo user : list){
// System.out.println(user.toString()+":"+user.getC());
// }
// System.out.println("---------按C升序-----------------");
// sortC(list,"asc");
// //按C排序
// for(UserInfo user : list){
// System.out.println(user.toString()+":"+user.getC());
// }
System.out.println("---------按D倒序-----------------");
sortD(list, "desc");
for(UserInfo user : list){
System.out.println(user.toString()+":"+user.getD());
}
System.out.println("---------按D升序序-----------------");
sortD(list, "asc");
for(UserInfo user : list){
System.out.println(user.toString()+":"+user.getD());
}
}
public static void sortC(List list,String type){
if("asc".equals(type)){
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Double a = ((UserInfo) o1).getC();
Double b = ((UserInfo) o2).getC();
return (int) (a-b);
}
});
}else{
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Double a = ((UserInfo) o1).getC();
Double b = ((UserInfo) o2).getC();
return (int) (b-a);
}
});
}
}
public static void sortD(List list,String type){
if("asc".equals(type)){
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
String a = ((UserInfo) o1).getD();
int a1 = Integer.valueOf(a.split(":")[0]);
String b = ((UserInfo) o2).getD();
int b1 = Integer.valueOf(b.split(":")[0]);
return a1-b1;
}
});
}else{
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
String a = ((UserInfo) o1).getD();
int a1 = Integer.valueOf(a.split(":")[0]);
String b = ((UserInfo) o2).getD();
int b1 = Integer.valueOf(b.split(":")[0]);
return b1-a1;
}
});
}
}
}
class UserInfo{
private int a;
private String b;
private Date time;
private Double c;
private String d;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public Double getC() {
return c;
}
public void setC(Double c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
public UserInfo(int a, String b, Date time, Double c, String d) {
super();
this.a = a;
this.b = b;
this.time = time;
this.c = c;
this.d = d;
}
}
按对象的属性进行排序