怎样按日期排序LIST集合???
LIST集合是从多个表查询的数据
需要按集合中数据的日期时间排序???不知有何好的方法?
效果:
      日期                姓名    性别
      2010-1-10 22:22     李xx      男
      2010-5-1  10:11     孙xx      男
      2010-3-22 21:11     王xx      女
      2010-3-22 20:09     姜xx      女
排后:
      2010-1-10 22:22     李xx      男
      2010-3-22 20:09     姜xx      女
      2010-3-22 21:11     王xx      女
      2010-5-1  10:11     孙xx      男
------解决方案--------------------如果是在查詢數據庫時進行排序,可以在查詢語句中加上order by日期字段。如果是對一個List進行排序,可先編寫comparator排序的方法,然後再用Collections.sort(list, comparator)實現。不知對你有無幫助。
------解决方案--------------------
你只要自定义一个方法就可以了
例子如下:
排序的方法类
import java.util.Comparator;
public class A implements Comparator<Object>{
	public int compare(Object o1, Object o2) {
		C c1=(C)o1;
		C c2=(C)o2;
		int flag=c1.getStuDate().compareTo(c2.getStuDate());		 
		return flag;	 	
	}
}
实体类
public class C {
	private String stuDate;
	private String stuName;
	private String stuSex;
	/**
	 * @return the stuName
	 */
	public String getStuName() {
		return stuName;
	}
	/**
	 * @return the stuDate
	 */
	public String getStuDate() {
		return stuDate;
	}
	/**
	 * @param stuDate the stuDate to set
	 */
	public void setStuDate(String stuDate) {
		this.stuDate = stuDate;
	}
	/**
	 * @param stuName the stuName to set
	 */
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	/**
	 * @return the stuSex
	 */
	public String getStuSex() {
		return stuSex;
	}
	/**
	 * @param stuSex the stuSex to set
	 */
	public void setStuSex(String stuSex) {
		this.stuSex = stuSex;
	}	
}
测试类
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class B {
	public static void main(String[] args) {        	      
		List<C> list = new ArrayList<C>();
		C c1= new C();
		c1.setStuDate("2010-1-10 22:22");
		c1.setStuName("李xx");
		c1.setStuSex("男");
		list.add(c1);		
		C c2= new C();
		c2.setStuDate("2010-5-1 10:11");
		c2.setStuName("孙xx");
		c2.setStuSex("男");
		list.add(c2);		
		C c3= new C();
		c3.setStuDate("2010-3-22 21:11");
		c3.setStuName("王xx");
		c3.setStuSex("女");
		list.add(c3);		
		C c4= new C();
		c4.setStuDate("2010-3-22 20:09");
		c4.setStuName("姜xx");
		c4.setStuSex("女");
		list.add(c4);		
		A cm = new A();
		Collections.sort(list,cm);
		for(int i=0;i<list.size();i++){
			C c=list.get(i);
			System.out.println(c.getStuDate()+" "+c.getStuName()+"  "+c.getStuSex());
		}
	}
}
第一次你可以把
A cm = new A();
Collections.sort(list,cm);
这两行代码注释上,就是按添加顺序输出
结果:
2010-1-10 22:22 李xx  男
2010-5-1 10:11 孙xx  男
2010-3-22 21:11 王xx  女
2010-3-22 20:09 姜xx  女
第二次你把这两行释放
就是排好序号的输出了
结果:
2010-1-10 22:22 李xx  男
2010-3-22 20:09 姜xx  女
2010-3-22 21:11 王xx  女
2010-5-1 10:11 孙xx  男
------解决方案--------------------用Collections.sort(list)方法就行啦,
這是我原來寫的
Java code
public class CreateTime implements Comparable{
    private int index;
    private Date date;
    
    public CreateTime(){
        Collections.sort(list)
    }
    
    public CreateTime(int index,Date date){
        t