List对象排序

参照http://jardot.iteye.com/blog/762349

定义一个实体UserInfo

package com.test;

import java.util.Date;

public class UserInfo {

	Integer  userId;
	String userName;
	Date birthDate;
	Integer  age;



	public Integer  getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public Integer  getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public UserInfo(Integer  id, String name, Date date, Integer  age) {
		super();
		this.userId = id;
		this.userName = name;
		this.birthDate = date;
		this.age = age;
	}

	@Override
	public String toString() {
		return "UserInfo [userId=" + userId + ", userName=" + userName + ", birthDate=" + birthDate + ", age=" + age + "]";
	}

}

 通用类:SortList

package com.test;

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

public class SortList<E>{	
	public void Sort(List<E> list, final String method, final String sort){
		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;
		    }
		 });
	}
}

 测试类:

package com.test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ListSort {
	public static void main(String[] args) throws Exception {
		List<UserInfo> list = new ArrayList<UserInfo>();

		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
		formater.parse("1980-12-01");
		list.add(new UserInfo(3, "b", formater.parse("1980-12-01"), 11));
		list.add(new UserInfo(1, "c", formater.parse("1980-10-01"), 30));
		list.add(new UserInfo(2, "a", formater.parse("1973-10-01"), 11));

		System.out.println("-------原来序列-------------------");
		for (UserInfo user : list) {
			System.out.println(user.toString());
		}

		//调用排序通用类  
		SortList<UserInfo> sortList = new SortList<UserInfo>();

		//按userId排序  
		sortList.Sort(list, "getUserId", "desc");
		System.out.println("--------按userId倒序------------------");
		for (UserInfo user : list) {
			System.out.println(user.toString());
		}

		//按userName排序  
		sortList.Sort(list, "getUserName", null);
		System.out.println("---------按username排序-----------------");
		for (UserInfo user : list) {
			System.out.println(user.toString());
		}

		//按birthDate排序  
		sortList.Sort(list, "getBirthDate", null);
		System.out.println("---------按birthDate排序-----------------");
		for (UserInfo user : list) {
			System.out.println(user.toString());
		}

		//按birthDate排序  
		sortList.Sort(list, "getAge", null);
		System.out.println("---------按age排序-----------------");
		for (UserInfo user : list) {
			System.out.println(user.toString());
		}

	}
}

 

 

你可能感兴趣的:(list)