1. 程式人生 > >android collection.sort()根據時間排序list

android collection.sort()根據時間排序list

首先:定義bean

然後:定義比較器

最後:測試使用

一.userBean

[java] view plaincopyprint?
  1. package com.butterfly.Class; 
  2. publicclass user { 
  3.     private String name; 
  4.     private String sex; 
  5.     privateint age; 
  6.     private String birthday; 
  7.     public user(String name,String sex,int age,String birthday){ 
  8.         this
    .name = name; 
  9.         this.sex = sex; 
  10.         this.age = age; 
  11.         this.birthday = birthday; 
  12.     } 
  13.     public String getName() { 
  14.         return name; 
  15.     } 
  16.     publicvoid setName(String name) { 
  17.         this.name = name; 
  18.     } 
  19.     public String getSex() { 
  20.         return sex; 
  21.     } 
  22.     public
    void setSex(String sex) { 
  23.         this.sex = sex; 
  24.     } 
  25.     publicint getAge() { 
  26.         return age; 
  27.     } 
  28.     publicvoid setAge(int age) { 
  29.         this.age = age; 
  30.     } 
  31.     public String getBirthday() { 
  32.         return birthday; 
  33.     } 
  34.     publicvoid setBirthday(String birthday) { 
  35.         this
    .birthday = birthday; 
  36.     } 
package com.butterfly.Class;

public class user {
	private String name;
	private String sex;
	private int age;
	private String birthday;
	
	public user(String name,String sex,int age,String birthday){
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.birthday = birthday;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	
	
}


二.定義比較器

[java] view plaincopyprint?
  1. package com.butterfly.Class; 
  2. import java.util.Comparator; 
  3. publicclass sortClass implements Comparator{ 
  4.     publicint compare(Object arg0,Object arg1){ 
  5.         user user0 = (user)arg0; 
  6.         user user1 = (user)arg1; 
  7.         int flag = user0.getBirthday().compareTo(user1.getBirthday()); 
  8.         return flag; 
  9.     } 
package com.butterfly.Class;

import java.util.Comparator;

public class sortClass implements Comparator{
	public int compare(Object arg0,Object arg1){
		user user0 = (user)arg0;
		user user1 = (user)arg1;
		int flag = user0.getBirthday().compareTo(user1.getBirthday());
		return flag;
	}
}


三.測試使用

[java] view plaincopyprint?
  1. package com.butterfly.Class; 
  2. import java.util.ArrayList; 
  3. import java.util.Collections; 
  4. import java.util.List; 
  5. publicclass testClass { 
  6.     publicstaticvoid main(String[] args){ 
  7.         List userList = new ArrayList(); 
  8.         userList.add(new user("吳光雷","男",25,"19871028")); 
  9.         userList.add(new user("吳橋","男",22,"19900923")); 
  10.         userList.add(new user("陸曉敏","男",25,"19870728")); 
  11.         userList.add(new user("王佳俊","男",23,"19890728")); 
  12.         userList.add(new user("秦菁","女",23,"19891212")); 
  13.         sortClass sort = new sortClass(); 
  14.         Collections.sort(userList,sort); 
  15.         for(int i=0;i<userList.size();i++){ 
  16.             user temp = (user)userList.get(i); 
  17.             System.out.println("姓名:"+temp.getName()+",生日:"+temp.getBirthday()); 
  18.         } 
  19.     } 
package com.butterfly.Class;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class testClass {
	public static void main(String[] args){
		List userList = new ArrayList();
		userList.add(new user("吳光雷","男",25,"19871028"));
		userList.add(new user("吳橋","男",22,"19900923"));
		userList.add(new user("陸曉敏","男",25,"19870728"));
		userList.add(new user("王佳俊","男",23,"19890728"));
		userList.add(new user("秦菁","女",23,"19891212"));
		
		sortClass sort = new sortClass();
		Collections.sort(userList,sort);
		for(int i=0;i<userList.size();i++){
			user temp = (user)userList.get(i);
			System.out.println("姓名:"+temp.getName()+",生日:"+temp.getBirthday());
		}
	}
}


輸出結果:

姓名:陸曉敏,生日:19870728
姓名:吳光雷,生日:19871028
姓名:王佳俊,生日:19890728
姓名:秦菁,生日:19891212
姓名:吳橋,生日:19900923