1. 程式人生 > >android Collections.sort排序的一種使用

android Collections.sort排序的一種使用

//按照javabean的時間排序
Collections.sort(infosList, new Comparator<Dzbp_Info>() {

    @Override
    public int compare(Dzbp_Info lhs, Dzbp_Info rhs) {
        try {
            Date d1 = null;
            Date d2 = null;
            if (!"".equals(lhs.getTime()) && !"".equals(rhs.getTime())) {
                d1 = dateFormat.parse(lhs.getTime());
                d2 = dateFormat.parse(rhs.getTime());
                return d1.getTime() - d2.getTime();
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
});
public class Dzbp_Info implements  Serializable{
   
   private String studentId;//學生ID
   private String studentName;//學生名稱
   private String classId;//班級id
   private String className;//班級名稱
   private String photo;//照片
   private String photoName;//照片名稱
   private String time;//建立時間
   private String content;//留言內容
   private String type;//資料型別(1:文字 / 2 :圖片)
   private boolean flag;//識別符號,留言是否已讀
   private Uri uri;
   
   
   public String getStudentId() {
      return studentId;
   }
   public void setStudentId(String studentId) {
      this.studentId = studentId;
   }
   public String getStudentName() {
      return studentName;
   }
   public void setStudentName(String studentName) {
      this.studentName = studentName;
   }
   public String getClassId() {
      return classId;
   }
   public void setClassId(String classId) {
      this.classId = classId;
   }
   public String getClassName() {
      return className;
   }
   public void setClassName(String className) {
      this.className = className;
   }
   public String getPhoto() {
      return photo;
   }
   public void setPhoto(String photo) {
      this.photo = photo;
   }
   public String getPhotoName() {
      return photoName;
   }
   public void setPhotoName(String photoName) {
      this.photoName = photoName;
   }
   public String getTime() {
      return time;
   }
   public void setTime(String time) {
      this.time = time;
   }
   public String getContent() {
      return content;
   }
   public void setContent(String content) {
      this.content = content;
   }
   public String getType() {
      return type;
   }
   public void setType(String type) {
      this.type = type;
   }
   public boolean isFlag() {
      return flag;
   }
   public void setFlag(boolean flag) {
      this.flag = flag;
   }
   public Uri getUri() {
      return uri;
   }
   public void setUri(Uri uri) {
      this.uri = uri;
   }

}





public int compare(Object o1, Object o2) 返回一個基本型別的整型
如果要按照升序排序,
則o1 小於o2,返回-1(負數),相等返回0,01大於02返回1(正數)
如果要按照降序排序
 則o1 小於o2,返回1(正數),相等返回0,01大於02返回-1(負數)